PAT 1057 Stack [难][树状数组]
1057 Stack (30)(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 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 (<= 10^5^). 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 10^5^.
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
题目大意:栈原本有push和pop的操作,现在加入了PeekMedian 即输出中值元素的操作,输入命令,输出相应的结果。
//关键就是怎么求中值不会导致超时。
//其实就是实现一种数据结构,能够查询头,弹出头,求中值。
代码来自:https://www.liuchuo.net/archives/2265
#include <iostream>
#include <stack>
#define lowbit(i) ((i) & (-i))
const int maxn = ;
using namespace std;
int c[maxn];//这个是用来计数每个元素出现的次数,比如push 3,那么就是3多了一个。
//表示≤当前下标的有几个。
stack<int> s;
void update(int x, int v) {
for(int i = x; i < maxn; i += lowbit(i))//更新这么多的吗?
c[i] += v;
}
int getsum(int x) {
int sum = ;
for(int i = x; i >= ; i -= lowbit(i))
sum += c[i];
return sum;
}
void PeekMedian() {
int left = , right = maxn, mid, k = (s.size() + ) / ;
while(left < right) {//使用二分法查找。
mid = (left + right) / ;
if(getsum(mid) >= k)
right = mid;
else
left = mid + ;
}
printf("%d\n", left);
}
int main() {
int n, temp;
scanf("%d", &n);
char str[];
for(int i = ; i < n; i++) {
scanf("%s", str);
if(str[] == 'u') {//这样判断比较快
scanf("%d", &temp);
s.push(temp);
update(temp, );//更新
} else if(str[] == 'o') {
if(!s.empty()) {
update(s.top(), -);
printf("%d\n", s.top());
s.pop();
} else {
printf("Invalid\n");
}
} else {
if(!s.empty())
PeekMedian();
else
printf("Invalid\n");
}
}
return ;
}
//这真的相当厉害,叹为观止。太厉害了。
1.取中值并没有采取对数据进行排序的思想,而是使用树状数组存储下标,下标就是数据的值。c[i]便表示≤当前值的数的个数。
2.当push时对其进行更新,更新的参数为1,即当前数+1.为了getSum函数,所以对其他包含当前的数也进行更新。
3.当pop时操作的是栈顶的元素,更新的参数为-1。
PAT 1057 Stack [难][树状数组]的更多相关文章
- PAT甲级1057 Stack【树状数组】【二分】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...
- PAT甲级题解-1057. Stack (30)-树状数组
不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...
- 1057. Stack (30) - 树状数组
题目如下: Stack is one of the most fundamental data structures, which is based on the principle of Last ...
- pat 甲级 1057 Stack(30) (树状数组+二分)
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...
- PAT-1057 Stack (树状数组 + 二分查找)
1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...
- PAT1057 Stack(树状数组+倍增)
目录 题目大意 题目分析 题目大意 要求维护一个栈,提供压栈.弹栈以及求栈内中位数的操作(当栈内元素\(n\)为偶数时,只是求第\(n/2\)个元素而非中间两数的平均值).最多操作100000次,压栈 ...
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
- 1057 Stack 树状数组
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- 1057 Stack (30分)(树状数组+二分)
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
随机推荐
- 发布订阅者模式之C#委托实现
1 ...
- SQLServer------如何让标识列重新开始计算
方法: DBCC CHECKIDENT (表名, RESEED, )
- Android 中如何从一个App启动另外一个App(如启动支付界面、启动地图界面、应用商场下载App等场景)
假定两个App,分别是A和B,当A运行某个功能需要启动B,一种是启动B应用,一种直接进入B的某个Activity.搜了很多资料,没有一个完整的.下面就A--Android5.1.1.B--Androi ...
- docker n2n安装与调试
docker n2n安装与调试 yum install -y docker docker pull pahud/n2n-docker cd / 10 mkdir data 11 cd data 12 ...
- Mybatis中的if test 标签
<if test='patrolResult != null and patrolResult != "" and patrolResult !="1"' ...
- java高级---->Thread之Condition的使用
Condition 将 Object 监视器方法(wait.notify 和 notifyAll)分解成截然不同的对象,以便通过将这些对象与任意 Lock 实现组合使用,为每个对象提供多个等待 set ...
- java基础---->Java关于复制的使用(一)
这里简单记录一下java中关于浅复制和深复制的知识.很多时候,一个人选择了行走,不是因为欲望,也并非诱惑,他仅仅是听到了自己内心的声音. java中的复制clone方法 一.java对象的浅复制 一个 ...
- latest报错
报错: 解决办法: 安装 babel-preset-latest
- Intel产品AMT本地及远程提权漏洞(CVE-2017-5689)复现 【转载自freebuf.com】
零.绪论: 1.鸣谢freebuf的文章,主要是学习这个漏洞,文章地址: Intel产品AMT本地及远程提权漏洞(CVE-2017-5689)复现 2.在shadon上找了多个该漏洞尝试复现失败(评论 ...
- 【BZOJ2157】旅游 树链剖分+线段树
[BZOJ2157]旅游 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本 ...