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. Python2.7-zipfile

    zipfile模块,提供了基本操作后缀为“zip”的文件的接口,一般使用 ZipFile 类完成操作 1.模块方法 zipfile.is_zipfile(filename):判断 filename 是 ...

  2. openJDK环境搭建编译(fedora)

    1.安装VMware  VMware-workstation-full-10.0.7-2844087.exe    破解码:HY06L-F334P-9Z6H9-6R2XM-23C6J  安装完成之后, ...

  3. Jmeter上传下载文件

    每次使用时都会忘记,此处是存储网路上通用的方式.   1.上传文件 记得勾选“use multipart/form-data for post”,表明此请求包含文件信息.在信息请求头中,需加入“Con ...

  4. C内存管理相关内容--取自高质量C++&C编程指南

    1.内存分配方式 内存分配方式有三种: (1)从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量,static变量. (2) 在栈上创建.在执行函数 ...

  5. Android 一s个相对完整的自动升级功能实现代码

    由于项目的需要最近做了一个关于Android自动升级的功能,下面将贴出Android手机客户端的完整代码.这段代码参考别的代码居多,由于不满足需求,所以自己仅仅改了一些需要变动的内容,其他功能都是按照 ...

  6. c# speech 文本转语言

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  7. 2017-2018-4 20155203《网络对抗技术》Exp3 免杀原理与实践

    1.基础问题回答 (1)杀软是如何检测出恶意代码的? 分析恶意程序的行为特征,分析其代码流将其性质归类于恶意代码 (2)免杀是做什么? 使恶意代码避免被查杀,也就是要掩盖恶意代码的特征 (3)免杀的基 ...

  8. mongodb安装教程

    MongoDB 下载及安装 MongoDB 提供了可用于 32 位和 64 位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoDB 预编译二进制包下载地址:https://www. ...

  9. 架构师修炼 II - 表达思维与驾驭方法论

    开篇之前我想先说说当年开发的那点事儿:大约10年前吧,我还是一个程序员的时候经常都是遇到这样的项目开发流程: 解决方案 :满足客户目的和投标用的一堆文档(不少还是互联网上抄的) ,是以Word为主的纯 ...

  10. 【转载】SSD断电保护原理

    异常掉电的隐患 若没有合理的掉电保护机制,而异常掉电的发生又不可避免,当发生异常掉电,会引发很多问题. (1)丢盘 异常掉电,会使得映射表来不及保存,丢失逻辑地址到物理地址的映射,待重新上电后,SSD ...