树状数组+二分。

#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)的更多相关文章

  1. PAT甲级题解-1057. Stack (30)-树状数组

    不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...

  2. 【PAT甲级】1057 Stack (30 分)(分块)

    题意: 输入一个正整数N(<=1e5),接着输入N行字符串,模拟栈的操作,非入栈操作时输出中位数.(总数为偶数时输入偏小的) trick: 分块操作节约时间 AAAAAccepted code: ...

  3. PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

    1057 Stack (30 分)   Stack is one of the most fundamental data structures, which is based on the prin ...

  4. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  5. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

  6. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  7. 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 ...

  8. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  9. 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 ...

随机推荐

  1. php file取重复

    function FetchRepeatMemberInArray($array) { // 获取去掉重复数据的数组 $unique_arr = array_unique ( $array ); // ...

  2. JS总结之一:字符串的调用方法

    字符串的调用方法:var s="hello, world";document.write(s.charAt(0)); //第一个字符document.write(s.charAt( ...

  3. Servlet基础知识

    基本知识一.Web结构1.两种应用程序 ①桌面应用程序:QQ.CS.MyEclipse.Office.DW.360.浏览器等必须下载.安装.桌面快捷方式.注册表信息.操作系统后台服务.占用操作系统端口 ...

  4. LeetCode OJ 222. Count Complete Tree Nodes

    Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...

  5. java FLOAT

    System.out.println(""+ 1/2); 得不到0.5,只能得到0. 要想打印出浮点数,必须除数和被除数至少有一个是浮点数,像这样: System.out.prin ...

  6. 网络请求 get post

    1.新建一个网络请求工具类,负责整个项目中所有的Http网络请求 提示:同步请求会卡住线程,发送网络请求应该使用异步请求(这意味着类方法不能有返回值) 2.工具类的实现 YYHttpTool.h文件 ...

  7. 初识mongo

    进入mongo /usr/local/mongodb/bin/mongo --host 查看所有db show dbs 查看当前进入的db db 查看当前db的所有collection show co ...

  8. xcode单步调试快捷键

    xcode单步调试快捷键f6,但是磨人f6是用来调节键盘亮度的,在系统偏好,键盘设置里面,做如下设置就可以了.

  9. Let'sencrypt认证的网站Https配置

    推荐使用这个脚本,具体说明里面都有 https://github.com/xdtianyu/scripts/tree/master/le-dns 它是通过调用dns服务商的api更新txt记录实现,无 ...

  10. jquery 高亮

    <ul> <li id="0">冬瓜很好吃</li> <li id="1">西瓜不好吃</li> & ...