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 std; const int maxn=+;
int n;
int c[maxn];
stack<int>S; int lowbit(int x){
return x&(-x);
} int getsum(int pos)
{
int res=;
while(pos>)
{
res=res+c[pos];
pos=pos-lowbit(pos);
}
return res;
} void update(int pos,int val)
{
while(pos<=)
{
c[pos]=c[pos]+val;
pos=pos+lowbit(pos);
}
} int main()
{
while(!S.empty()) S.pop();
memset(c,,sizeof c);
scanf("%d",&n);
int sz=;
for(int i=;i<=n;i++)
{
char op[]; scanf("%s",op);
if(strcmp(op,"Pop")==)
{
if(sz==) printf("Invalid\n");
else
{
sz--;
printf("%d\n",S.top());
update(S.top(),-);
S.pop();
}
}
else if(strcmp(op,"PeekMedian")==)
{
if(sz==) printf("Invalid\n");
else
{
int l=,r=;
int ans;
while(l<=r)
{
int mid=(l+r)/;
if(getsum(mid)>=(sz+)/)
{
ans=mid;
r=mid-;
}
else l=mid+;
}
printf("%d\n",ans);
}
}
else
{
int num; scanf("%d",&num);
update(num,);
sz++;
S.push(num);
}
}
return ;
}
PAT (Advanced Level) 1057. Stack (30)的更多相关文章
- PAT甲级题解-1057. Stack (30)-树状数组
不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...
- 【PAT甲级】1057 Stack (30 分)(分块)
题意: 输入一个正整数N(<=1e5),接着输入N行字符串,模拟栈的操作,非入栈操作时输出中位数.(总数为偶数时输入偏小的) trick: 分块操作节约时间 AAAAAccepted code: ...
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT (Advanced Level) Practice 1001-1005
PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...
- PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...
- PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642
PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642 题目描述: At the beginning of ever ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642
PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...
随机推荐
- ReactiveCocoa常用方法
//1 代替kvo [[self.redView rac_valuesForKeyPath:@"frame" observer:nil] subscribeNext:^(id x) ...
- Linux系统手动安装rpm包依赖关系分析(以Kernel升级为例)
有在Linux系统中安装软件的经历的人都知道,在Linux系统中手动安装软件不想在Windows下安装软件那么方便,直接双击,然后下一步下一步就可以把软件成功的装入到系统中,而在Linux系统中,安装 ...
- IoC容器Autofac正篇之类型关联(服务暴露)(八)
类型关联 类型关联就是将类挂载到接口(一个或多个)上去,以方便外部以统一的方式进行调用(看下例). 一.As关联 我们在进行手动关联时,基本都是使用As进行关联的. 1 2 3 4 5 6 7 8 ...
- AFNetWorking 判断当前版本是否是最新版本
NSString *url = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",@ ...
- docker installation on ubuntu
Ubuntu Docker is supported on these Ubuntu operating systems: Ubuntu Xenial 16.04 (LTS) Ubuntu Trust ...
- mongodb常见问题
1.count统计结果错误 这是由于分布式集群正在迁移数据,它导致count结果值错误,需要使用aggregate pipeline来得到正确统计结果,例如: db.collection.aggreg ...
- 【单源最短路模板】 poj 2387
#include <cstdio> #include <iostream> #include <stdlib.h> #include <memory.h> ...
- LightOJ 1030 Discovering Gold 数学期望计算
题目大意:给出长度为n的一条隧道,每个位置都有一定数量的财宝.给你一枚骰子,roll到几点就前进几步,如果即将到达的地方超过了这条隧道长度,就重新roll一次,走到n点结束.求这个过程能收获多少财宝. ...
- Temporary exceptions can be configured via your app's Info.plist file.
报错: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure ...
- PARTITION BY 和 group by
sum() over (PARTITION BY ...) 是一个分析函数. 他执行的效果跟普通的sum ...group by ...不一样,它计算组中表达式的累 ...