PAT 1057 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 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 (-th smallest element if N is even, or (-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 (≤). 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 1.

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 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<stack>
 5 #define lowbit(x) ((x)&(-x))
 6 using namespace std;
 7
 8 vector<int> C(100010, 0);
 9
10 stack<int> s;
11 int getSum(int x){
12   int sum=0;
13   for(int i=x; i>0; i -= lowbit(i))
14     sum += C[i];
15   return sum;
16 }
17
18 void update(int x, int v){
19   for(int i=x; i<100010; i += lowbit(i))
20     C[i] += v;
21 }
22
23 void Media(){
24   int l=1, r=100010;
25   int k=(s.size()+1)/2;
26   while(l<r){
27     int mid=(l+r)/2;
28     if(getSum(mid)>=k) r=mid;
29     else if(getSum(mid)<k) l=mid+1;
30   }
31   printf("%d\n", l);
32 }
33 int main(){
34   int n, i;
35   scanf("%d", &n);
36
37   for(i=0; i<n; i++){
38     char ch[15];
39     scanf("%s", ch);
40     if(ch[1]==‘o‘){
41       if(!s.size()) printf("Invalid\n");
42       else{
43         printf("%d\n", s.top());
44         update(s.top(), -1);
45         s.pop();
46       }
47     }else if(ch[1]==‘u‘){
48       int temp;
49       scanf("%d", &temp);
50       s.push(temp);
51       update(temp, 1);
52     }else{
53       if(s.size()) Media();
54       else printf("Invalid\n");
55     }
56   }
57   return 0;
58 }

原文地址:https://www.cnblogs.com/mr-stn/p/9574616.html

时间: 2024-11-08 02:16:09

PAT 1057 Stack的相关文章

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

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

PAT 甲级 1057 Stack

https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 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 o

PAT甲级1057 Stack【树状数组】【二分】

题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和找中位数三种操作. 思路: 好久没写题.感觉傻逼题写多了稍微有点数据结构的都不会写了. pop和push操作就不说了. 找中位数的话就二分去找某一个数前面一共有多少小于他的数,找到那个小于他的数刚好等于一半的. 找的过程中要用到前缀和,所以自然而然就应该上树状数组. 要注意树状数组的界应该是1e5而

PAT (Advanced Level) 1057 Stack

题解 第一种方法:令数组tree[]记录栈中的元素,栈中的数值 x 的个数为 tree[x] .树状数组维护tree[],然后二分查找.   第二种方法:利用分块,以一定长度区间为单位,记录栈中数值的个数,然后暴力查找. 代码 //树状数组 + 二分 #include<bits/stdc++.h> using namespace std; const int maxn=100010; int tree[maxn]; stack<int> sta; void update(int p

PAT——1057. 数零壹

给定一串长度不超过105的字符串,本题要求你将其中所有英文字母的序号(字母a-z对应序号1-26,不分大小写)相加,得到整数N,然后再分析一下N的二进制表示中有多少0.多少1.例如给定字符串"PAT (Basic)",其字母序号之和为:16+1+20+2+1+19+9+3=71,而71的二进制是1000111,即有3个0.4个1. 输入格式: 输入在一行中给出长度不超过105.以回车结束的字符串. 输出格式: 在一行中先后输出0的个数和1的个数,其间以空格分隔. 输入样例: PAT (

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