1057. Stack (30)-PAT
来源:互联网 发布:mysql 5.0.45中文版 编辑:程序博客网 时间:2024/05/22 00:34
1057. Stack (30)-中位数-BIT
Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed to implement a stack with an extra operation: PeekMedian -- return the median value of all the elements in the stack. With N elements, the median value is defined to be the (N/2)-th smallest element if N is even, or ((N+1)/2)-th if N is odd.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<= 105). Then N lines follow, each contains a command in one of the following 3 formats:
Push keyPop
PeekMedian
where key is a positive integer no more than 105.
Output Specification:
For each Push command, insert key into the stack and output nothing. For each Pop or PeekMedian command, print in a line the corresponding returned value. If the command is invalid, print "Invalid" instead.
Sample Input:17PopPeekMedianPush 3PeekMedianPush 2PeekMedianPush 1PeekMedianPopPopPush 5Push 4PeekMedianPopPopPopPopSample Output:
InvalidInvalid322124453Invalid
推荐指数※※
来源:http://pat.zju.edu.cn/contests/pat-a-practise/1057
这道题如果不考虑时间限制,那是比较简单的,一开始就直接使用一个数组计数,使用二分法搜索,但都是超时。
这样只能用上统计累计频次比较好的Bianry Indexed Tree(http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees).
还有这里cin,cout还是不要用了,效率不如printf和scanf。
#include<iostream>#include<string.h>#include<stdio.h>using namespace std;#define Maxval 100009int tree[Maxval+1];int stack[Maxval+1];void update(int idx,int val){while(idx<Maxval){tree[idx]+=val;idx+=(idx&-idx);}}int read(int idx){int sum=0;while(idx>0){sum+=tree[idx];idx-=(idx&-idx);}return sum;}int find_cumulate(int cumfre){int low=0,high=Maxval,mid;while(low<high-1){if((low+high)&1==1)mid=(low+high-1)/2;elsemid=(low+high)/2;int sum=read(mid);if(sum>=cumfre)high=mid;else low=mid;}return high;}int main(){int n,i;char str[20];scanf("%d",&n);long tmp,top;memset(tree,0,Maxval*sizeof(int));memset(stack,0,Maxval*sizeof(int));top=0;for(i=0;i<n;i++){scanf("%str",str);if(str[1]=='o'){//POPif(top>0){tmp=stack[top--];printf("%d\n",tmp);update(tmp,-1);}elseprintf("Invalid\n");}else if(str[1]=='u'){//pushcin>>tmp;stack[++top]=tmp;update(tmp,1);}else if(str[1]=='e'){//mediaif(top>0){if(top&1==1)tmp=(top+1)/2;elsetmp=top/2;tmp=find_cumulate(tmp);printf("%d\n",tmp);}elseprintf("Invalid\n");}else{printf("Invalid\n");}}return 0;}
这是我超时的代码,比较的朴素
#include<iostream>#include<string.h>#include<string>#include<stack>#include<vector>#include<set>#include<algorithm>#define N 100000#define Hash_N 10000using namespace std;long find_median(long *arr,long first,long last,long val){long mid;while(first<last&&first!=0&&last!=Hash_N-1){mid=(first+last)/2;if(arr[mid]==val){return mid;}else if(arr[mid]>val)last=mid;elsefirst=mid;}return first;}int main(){long n,i,j;char str[20];long totalnum,num[N],hash_num[Hash_N];stack<long> st;cin>>n;totalnum=0;long tmp;memset(num,0,N*sizeof(int));memset(hash_num,0,Hash_N*sizeof(int));long fhash=N/Hash_N;for(i=0;i<n;i++){cin>>str;if(strcmp(str,"Pop")==0){//POPif(!st.empty()){tmp=st.top();cout<<tmp<<endl;st.pop();totalnum--;num[tmp]--;for(j=tmp/fhash;j<Hash_N;j++)hash_num[j]--;}elsecout<<"Invalid"<<endl;}else if(strcmp(str,"Push")==0){//pushcin>>tmp;st.push(tmp);totalnum++;num[tmp]++;hash_num[tmp/fhash]++;}else{//mediaif(!st.empty()){if(totalnum&1==1)tmp=(totalnum+1)/2;elsetmp=totalnum/2;long tmp_sum=0,k;k=find_median(hash_num,0,Hash_N,tmp);if(k>0)tmp_sum=hash_num[k-1];elsetmp_sum=0;for(j=k*fhash;j<N;j++){tmp_sum+=num[j];if(tmp_sum>=tmp){cout<<j<<endl;break;}}}elsecout<<"Invalid"<<endl;}}return 0;}
- 1057. Stack (30)-PAT
- pat 1057. Stack (30)
- PAT 1057. Stack (30)
- 【PAT】1057. Stack (30)
- PAT 1057. Stack (30)
- PAT A 1057. Stack (30)
- PAT (Advanced) 1057. Stack (30)
- PAT(A) - 1057. Stack (30)
- 【PAT甲级】1057. Stack (30)
- PAT(A) - 1057. Stack (30)
- PAT甲级1057. Stack (30)
- PAT-A-1057. Stack (30)
- PAT 甲级 1057. Stack (30)
- 浙大2013复试:PAT 1057. Stack (30)
- PAT甲级练习1057. Stack (30)-未完成
- PAT 1057. Stack (30) 中位数;树状数组
- 【PAT】【Advanced Level】1057. Stack (30)
- PAT 1057. Stack
- ACM中
- 在Android系统中添加JAVA API
- Discuz纯DIY -- 【图片整体横向滚动 带左右方向键 for X1.5正式版】[又增加连续滚动]
- android平台下编译KO文件
- C#.NET的保护源码,防反编辑工具
- 1057. Stack (30)-PAT
- linux 下的UDP client/server 示例
- poj 1185 炮兵阵地 【状态压缩dp】
- Linux 下git的安装
- [Linux]Shell快速学习小总结[TBD]
- C++拷贝构造函数详解【这篇文章 写的真棒!!!】
- 在tomcat和eclipse进行远程调试的配置
- android的学习与查阅路线
- android中怎么对一个code进行加密