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 ...
随机推荐
- zen cart global $db 这噶哒
zen cart $db变量是mysql数据库类的对象,是一个全局变量.在zen cart系统里面的任何一个地方都可以使用这个对象来查询数据库获取数据库的内容.只要需要用到查询数据库,都可以使用这个对 ...
- 嵌套表SHAPE
SQL语法 SHAPE {<master query>} APPEND ({ <child table query> } RELATE <master column> ...
- 新建web project不自动生成web.xml解决方案
一步一步建立Web Project ,第3步会有 “Generate Web.xml deployment descriptor”,默认没勾选,勾上就行了
- hdu_5221_Occupation(树剖)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5221 题意:给你一棵树,每个节点有一定的值,有三种操作: 1 x y 表示占领树上x-y的所有节点,2 ...
- hdu_3555_Bomb(数位DP)
题目连接:hdu_3555_Bomb 题意:和2089一样,只是数据大了点,不过道理是一样的 #include<cstdio> #include<cstring> #defin ...
- SQL Server不能通过外部IP访问,解决方法
SQL Server不能通过外部IP访问,解决方法 版本:SQL server 2008 express with tools 打开配置管理器,开启 TCP,右键属性设置TCP端口: 设置 ...
- 给php代码添加规范的注释phpDocumentor
给php代码添加规范的注释更多参考 http://phpdoc.org/docs/latest/index.html在phpdocumentor中,注释分为文档性注释和非文档性注释.所谓文档性注释,是 ...
- Ansible3:ansible.cfg配置说明【转】
Ansible默认安装好后有一个配置文件/etc/ansible/ansible.cfg,该配置文件中定义了ansible的主机的默认配置部分,如默认是否需要输入密码.是否开启sudo认证.actio ...
- 设计一种前端数据延迟加载的jQuery插件(2)
背景 最近看到很多网站都运用到了一种前端数据延迟加载技术,包括淘宝,新浪网等等,这样做的目的可以使得一些未显示的图片随 着滚动条的滚动进行延迟显示. 好处显而易见,可以减少前端对于图片的Http请求, ...
- 对象之function
开头啰嗦几句:程序之中,万事万物皆对象,这就是面向对象编程的核心思想,比如我们的js中的function,其实也是一个对象,是对象就可以有属性,在jq的源码中经常看到类似这样的方法: jQuery.E ...