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 (N/2)-th smallest element if Nis 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 Nlines follow, each contains a command in one of the following 3 formats:
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.
知识点:树状数组,二分查找

二分查找的写法
对比两种写法:
正确:
find(x){
while(l!=r){
mid=(l+r)/2;
if(list[mid]>=x){
r=mid;
}else{
l=mid+1;
}
}
}
错误:
find(x){
	while(l!=r){
		mid=(l+r)/2;
		if(list[mid]>x){
			r=mid-1;
		}else{
			l=mid;
		}
	}
}

#include <iostream>
#include <stack>
using namespace std;
const int maxn = ; int n;
int c[maxn];
stack<int> st; int lowbit(int v){ // lowbit()函数取二进制数最后一个1
return v&(-v);
} 15 int getSum(int x){ // 往前加和
16 int sum=0;
17 for(int i=x;i>0;i-=lowbit(i)){
18 sum += c[i];
19 }
20 return sum;
21 } void upDate(int v,int p){ // 往后更新
for(int i=v;i<maxn;i+=lowbit(i)){
c[i]+=p;
}
} int findMid(){
printf("\n");
int mid=(st.size()+)/;
int l=, r=maxn-;
int cnt=;
while(l!=r&cnt<){ // 二分查找
cnt++;
printf("* %d %d\n",l,r);
int m=(l+r)/;
if(getSum(m)>=mid){
r=m;
}else{
l=m+;
}
}
return l;
} int main(int argc, char *argv[]) {
fill(c,c+maxn,); scanf("%d",&n);
char cmd[]; int key;
for(int i=;i<n;i++){
scanf("%s",cmd);
if(cmd[]=='u'){
scanf("%d",&key); st.push(key);
upDate(key,);
}else if(cmd[]=='o'){
if(st.size()==) printf("Invalid\n");
else{
upDate(st.top(),-);
printf("%d\n",st.top());
st.pop();
}
}else if(cmd[]=='e'){
if(st.size()==) printf("Invalid\n");
else{
printf("%d\n",findMid());
};
}
}
}
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
1057 Stack 树状数组的更多相关文章
- PAT 1057 Stack [难][树状数组]
		
1057 Stack (30)(30 分) Stack is one of the most fundamental data structures, which is based on the pr ...
 - PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
		
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
 - 1057. Stack (30) - 树状数组
		
题目如下: Stack is one of the most fundamental data structures, which is based on the principle of Last ...
 - 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 In Fir ...
 - 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次,压栈 ...
 - UVA 1513 Movie collection (树状数组+反向存储)
		
题意:给你n盘歌碟按照(1....n)从上到下放,接着m个询问,每一次拿出x碟,输出x上方有多少碟并将此碟放到开头 直接想其实就是一线段的区间更新,单点求值,但是根据题意我们可以这样想 首先我们倒着存 ...
 
随机推荐
- xml转化为Dictionary
			
代码 public SortedDictionary<string, object> FromXml(string xml) { SortedDictionary<string, o ...
 - [z]分区truncate操作的介绍及对全局索引和空间释放影响的案例解析
			
[z]https://www.2cto.com/database/201301/181226.html 环境: [sql] [oracle@localhost ~]$ uname -r 2.6.18- ...
 - TF30063:没有访问xxx的权限      vs2017
			
凭据什么的都删掉了,还是无法连接上,最后同事用一种方法o(╥﹏╥)o,先登录vs2015的tfs账号,然后再vs2017上面登录tfs账号即可解决.PS:网上很多文章都是抄的,这个Bug真TM恶心.
 - Windows下PythonQt编译(vs2015+Qt5.11.2+PythonQt 3.2)
			
后记: 由于自己low,没有下载罪行的python3.2导致编译上遇到种种问题,后文可以参考,建议看: <Windows7 VS2015 下编译 PythonQt3.2> https:// ...
 - u-boot之start_armboot函数分析
			
在分析start.S文件过程中提到过,最后从汇编跳到C函数执行的是start_armboot函数,位于lib_arm\board.c文件下,它的执行流程图如下,截图来源于<嵌入式LINUX应用开 ...
 - u-boot之make <board_name>_config执行过程分析
			
从网上下载uboot源码之后需要对源码作相应修改来支持自己的开发板,更改完源码之后需要配置.uboot(make<board_name>_config).这里以百问网的开发板jz2440为 ...
 - Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
			
最近在Tomcat上配置一个项目,在点击一个按钮,下载一个文件的时候,老是会报上面的错误.试了很多方法,如对server.xml文件中,增加MaxHttpHeaderSize的大小,改端口,改Tomc ...
 - GitHub上README.md教程   详情介绍 (修改图片连接地址错误)
			
最近对它的README.md文件颇为感兴趣.便写下这贴,帮助更多的还不会编写README文件的同学们. README文件后缀名为md.md是markdown的缩写,markdown是一种编辑博客的语言 ...
 - Java界面编程—布局管理
			
布局是指容器中组件的排列方式 常用的布局管理器 布局管理器名称 所属类包 说明 FlowLayout(流式布局) java.awt 组件按照加入的先后顺序.按照设置的对齐方式从左向右排列,一行排满后到 ...
 - keepalived配虚拟ip(vip)的作用
			
keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议. 虚拟路由冗余协议,可以认为是实现路由器高可用的 ...
 
			
		
