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 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

代码:

#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int N;
int c[maxn];
stack<int> s; int lowerbit(int x) {
return x & -x;
} void update(int x, int v) {
for(int i = x; i < maxn; i += lowerbit(i))
c[i] += v;
} int getsum(int x) {
int sum = 0;
for(int i = x; i >= 1; i -= lowerbit(i))
sum += c[i];
return sum;
} void PeekMedian() {
int left = 1, right = maxn, mid, k = (s.size() + 1) / 2;
while(left < right) {
mid = (left + right) / 2;
if(getsum(mid) >= k)
right = mid;
else left = mid + 1;
}
printf("%d\n", left);
} int main() {
scanf("%d", &N);
while(N --) {
string op;
cin >> op;
if(op == "Pop") {
if(s.empty()) printf("Invalid\n");
else {
printf("%d\n", s.top());
update(s.top(), -1);
s.pop();
}
} else if(op == "Push") {
int num;
scanf("%d", &num);
s.push(num);
update(num, 1);
} else {
if(s.empty()) printf("Invalid\n");
else PeekMedian();
}
}
return 0;
}

  树状数组 第一次写到树状数组 看博客还没看的很懂但是隐约感觉到这是一个模板题 emmmm 以后多做一点就会明白了叭 唉 哭唧唧

PAT 甲级 1057 Stack的更多相关文章

  1. PAT甲级1057. Stack

    PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...

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

    1057 Stack (30 分)   Stack is one of the most fundamental data structures, which is based on the prin ...

  3. pat 甲级 1057 Stack(30) (树状数组+二分)

    1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...

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

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...

  5. PAT甲级——A1057 Stack

    Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...

  6. PAT 1057 Stack [难][树状数组]

    1057 Stack (30)(30 分) Stack is one of the most fundamental data structures, which is based on the pr ...

  7. pat甲级题解(更新到1013)

    1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...

  8. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

随机推荐

  1. 【转】numpy中mean和average的区别

    转自:https://blog.csdn.net/Muzi_Water/article/details/85104941 mean和average都是计算均值的函数,在不指定权重的时候average和 ...

  2. snmpwalk,iptables

    -A RH-Firewall-1-INPUT -i lo -j ACCEPT -A INPUT -s 1.1.1.1 -p udp -d 2.2.2.2 --dport 161 -j ACCEPT - ...

  3. mybatis的mapper代理,SqlMapConfig.xml中配置,输入和输出映射使用案例

    public class User { private int id; private String username;// 用户姓名 private String sex;// 性别 private ...

  4. C++之new和delete操作符

    在C语言中的动态分配和释放内存的函数是malloc calloc 和 free , 而在C++中要用 new new[] delete delete[] 来申请动态空间和释放空间. 注意:的是new. ...

  5. 蓝桥杯历届试题 危险系数(dfs或者并查集求无向图关于两点的割点个数)

    Description 抗日战争时期,冀中平原的地道战曾发挥重要作用. 地道的多个站点间有通道连接,形成了庞大的网络.但也有隐患,当敌人发现了某个站点后,其它站点间可能因此会失去联系. 我们来定义一个 ...

  6. 报错Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA

    解决方法:import os                  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'输入1:显示所有信息 2:只显示warning和erro ...

  7. WPF EventTrigger,BeginStoryboard

    <Window x:Class="WpfApplication2.LoginWind" xmlns="http://schemas.microsoft.com/wi ...

  8. WFP page navigator control

    WPF navigator UI: <Grid x:Class="WpfApplication2.PagerNav" xmlns="http://schemas.m ...

  9. 汇编 REPNE/REPNZ 指令,SCASB 指令

    知识点: REPNE/REPNZ 指令 SCASB 指令 一.SCASB 指令 cmp byte ptr [edi],al //对标志位的影响相当于sub指令 //同时还会修改寄存器EDI的值:如 ...

  10. RHEL6 最小化系统 编译安装部署zabbix (mysql)

    RHEL6 最小化系统 编译安装部署zabbix (mysql)官方说明详细见:https://www.zabbix.com/documentation/4.0/manual/installation ...