pat1057. Stack (30)

1057. Stack (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 key
Pop
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:
17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop
Sample Output:
Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid

提交代码

 

注意点:

1.树状数组的常见操作

学习:

http://www.cnblogs.com/zhangshu/archive/2011/08/16/2141396.html

http://blog.csdn.net/eli850934234/article/details/8863839

 

 1 int lowbit(int val){
 2     return val&(-val);
 3 }
 4 int sum(int val){
 5     int res=0;
 6     while(val>0){
 7         res+=line[val];
 8         val-=lowbit(val);
 9     }
10     return res;
11 }
12 void add(int val,int x){
13     while(val<maxnum){
14         line[val]+=x;
15         val+=lowbit(val);
16     }
17 }
18 int find(int val){
19     int l=1,r=maxnum,mid,res;
20     while(l<r){//右边取不到
21         mid=(l+r)/2;
22         res=sum(mid);
23         if(res<val){
24             l=mid+1;//(1)
25         }
26         else{
27             r=mid;//[l,r)的每一点求sum,一定小于sum(r),最终一定终止于(1)。最后返回l。
28         }
29     }
30     return l;
31 }

这里要注意二分查找的书写。对于区间[a,b),不断二分,最后返回l。

 

2.string的操作一般要比char的操作时间长。这里用的string会超时。

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<stack>
 5 #include<set>
 6 #include<map>
 7 #include<algorithm>
 8 using namespace std;
 9 #define maxnum 100005
10 int line[maxnum];
11 int lowbit(int val){
12     return val&(-val);
13 }
14 int sum(int val){
15     int res=0;
16     while(val>0){
17         res+=line[val];
18         val-=lowbit(val);
19     }
20     return res;
21 }
22 void add(int val,int x){
23     while(val<maxnum){
24         line[val]+=x;
25         val+=lowbit(val);
26     }
27 }
28 int find(int val){
29     int l=1,r=maxnum,mid,res;
30     while(l<r){//右边取不到
31         mid=(l+r)/2;
32         res=sum(mid);
33         if(res<val){
34             l=mid+1;
35         }
36         else{
37             r=mid;
38         }
39     }
40     return l;
41 }
42 int main(){
43     //freopen("D:\\INPUT.txt","r",stdin);
44     int n,num;
45     char op[15];
46     //string op;
47     stack<int> s;
48     scanf("%d",&n);
49     while(n--){
50         scanf("%s",op);//cin>>op;
51         if(op[1]=='u'){
52             scanf("%d",&num);
53             s.push(num);
54             add(num,1);
55         }
56         else{
57             if(op[1]=='o'){
58                 if(s.empty()){
59                     printf("Invalid\n");
60                 }
61                 else{
62                     num=s.top();
63                     s.pop();
64                     printf("%d\n",num);
65                     add(num,-1);
66                 }
67             }
68             else{
69                 if(s.size()==0){
70                     printf("Invalid\n");
71                     continue;
72                 }
73                 if(s.size()%2==0){
74                     num=find(s.size()/2);
75                 }
76                 else{
77                     num=find((s.size()+1)/2);
78                 }
79                 printf("%d\n",num);
80             }
81         }
82     }
83     return 0;
84 }

 

posted @ 2015-09-08 01:34  Deribs4  阅读(278)  评论(0编辑  收藏  举报