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 }
时间: 2024-10-10 07:25:14

pat1057. Stack (30)的相关文章

PAT-1057. Stack (30)--树状数组

今天新学了一个知识,叫做线状数组,主要应用领域 1,数据频繁更新 2,求解某一段区间的和 以上产景情况下可以使用线状数组,更新某一个数据和求某一段时间之和时间复杂度都是Log(N) {常规情况是O(1)和O(N)} 线状数组和RMQ差不多,都可以再Log(N)时间复杂度内求解某一段区间的长度,线状数组额实现方式更为简单,更为方便 以下可以当做线段树的模板 主要函数有lowbit给定制定数字二进制下在末尾为0的个数. Update更新某一节点的值. getsum求解某一区间的值 Update是从叶

PAT 1057. Stack (30)

题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057 用树状数组和二分搜索解决,对于这种对时间复杂度要求高的题目,用C的输入输出显然更好 #include <cstdio> #include <string> #include <vector> #include <stack> using namespace std; const int NUM=100001; struct TreeArray {

PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

1057 Stack (30 分) 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). No

1057. Stack (30)

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

PAT1057. Stack

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

PAT甲题题解-1057. Stack (30)-树状数组

不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekMedian:返回stack中第(n+1)/2个小的数 建立一个栈来模拟push和pop,另外还需要树状数组,来统计栈中<=某个数的总个数不了解树状数组的建议学习一下,很有用的.树状数组为c,有个虚拟的a数组,a[i]表示i出现的次数sum(i)就是统计a[1]~a[i]的和,即1~i出现的次数当我要

PAT (Advanced Level) 1057. Stack (30)

树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using namespace

1057. Stack (30) - 树状数组

题目例如以下: 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

PAT1057 Stack(树状数组+倍增)

目录 题目大意 题目分析 题目大意 要求维护一个栈,提供压栈.弹栈以及求栈内中位数的操作(当栈内元素\(n\)为偶数时,只是求第\(n/2\)个元素而非中间两数的平均值).最多操作100000次,压栈的数字\(key\)范围是[1,100000]. 题目分析 前两个操作用\(stack\)就好. 求中位数.暴力做法即使用上优先队列也是稳稳的超时.考虑树状数组. 压栈时,将\(key\)值对应的位置加1.弹栈减1. 求中位数,可以二分求出\(sum[1:p]==(n+1)/2\)最小的\(p\),