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 ...
随机推荐
- Unity3d之将terrain转化成mesh
Unity3d中,terrain还是比较耗的,DrawCall数也比较多,为了优化性能,可能需要将terrain转化成mesh. 现提供一工具,思路是根据terrain高度图生成mesh等,可参考: ...
- Android面试题集锦 (转)
转自:http://xiechengfa.iteye.com/blog/1044721 一些常见的Android面试基础题做下总结,看看你能做出多少道? 1. Intent的几种有关Activity启 ...
- 新随笔ps泡泡制作
http://jingyan.baidu.com/article/4d58d5413568a79dd4e9c016.html
- VO , PO , BO , QO, DAO ,POJO
VO , PO , BO , QO, DAO ,POJO, O/R Mapping 是 Object Relational Mapping (对象关系映射)的缩写.通俗点讲,就是将对象与关系数据库绑定 ...
- ubuntu 自动获取ip的怎么设置
ubuntu以DHCP方式配置网卡自动获取ip编辑文件/etc/network/interfaces:sudo vi /etc/network/interfaces并用下面的行来替换有关eth0的行: ...
- Inno Setup入门(六)——在程序目录下创建文件
创建文件夹可以使用[dirs]段实现,代码如下: [setup] ;全局设置,本段必须 AppName=Test AppVerName=TEST DefaultDirName="E:\TES ...
- java中怎么解决路径中文的问题
在我遇到精灵线程的问题时,遇到一个中文路径的问题 原来是这样的 URL url=Test8.class.getClassLoader().getResource(""); Stri ...
- 查找List中的最大最小值
以下实例演示了如何使用 Collections 类的 max() 和 min() 方法来获取List中最大最小值: import java.util.*; public class Main { pu ...
- UVA 10308 Roads in the North
input u1 v1 w1 u2 v2 w2 ... un vn wn 1<=vi,ui<=n+1 /n output 距离最远的两个点的距离 做法:一颗全连通且只有一条路从一个顶点到达 ...
- 使用Core Animation对象来实现动画
转载保留原文地址:http://blog.csdn.net/kqjob/article/details/10417461,转载的 在iOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现, ...