1057. Stack (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 N is 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 N lines follow, each contains a command in one of the following 3 formats:

Push key
Pop
PeekMedian

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.

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

提交代码

注意点:

1.树状数组的常见操作

学习:

http://www.cnblogs.com/zhangshu/archive/2011/08/16/2141396.html

http://blog.csdn.net/eli850934234/article/details/8863839

 int lowbit(int val){
return val&(-val);
}
int sum(int val){
int res=;
while(val>){
res+=line[val];
val-=lowbit(val);
}
return res;
}
void add(int val,int x){
while(val<maxnum){
line[val]+=x;
val+=lowbit(val);
}
}
int find(int val){
int l=,r=maxnum,mid,res;
while(l<r){//右边取不到
mid=(l+r)/;
res=sum(mid);
if(res<val){
l=mid+;//(1)
}
else{
r=mid;//[l,r)的每一点求sum,一定小于sum(r),最终一定终止于(1)。最后返回l。
}
}
return l;
}

这里要注意二分查找的书写。对于区间[a,b),不断二分,最后返回l。

2.string的操作一般要比char的操作时间长。这里用的string会超时。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
#define maxnum 100005
int line[maxnum];
int lowbit(int val){
return val&(-val);
}
int sum(int val){
int res=;
while(val>){
res+=line[val];
val-=lowbit(val);
}
return res;
}
void add(int val,int x){
while(val<maxnum){
line[val]+=x;
val+=lowbit(val);
}
}
int find(int val){
int l=,r=maxnum,mid,res;
while(l<r){//右边取不到
mid=(l+r)/;
res=sum(mid);
if(res<val){
l=mid+;
}
else{
r=mid;
}
}
return l;
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,num;
char op[];
//string op;
stack<int> s;
scanf("%d",&n);
while(n--){
scanf("%s",op);//cin>>op;
if(op[]=='u'){
scanf("%d",&num);
s.push(num);
add(num,);
}
else{
if(op[]=='o'){
if(s.empty()){
printf("Invalid\n");
}
else{
num=s.top();
s.pop();
printf("%d\n",num);
add(num,-);
}
}
else{
if(s.size()==){
printf("Invalid\n");
continue;
}
if(s.size()%==){
num=find(s.size()/);
}
else{
num=find((s.size()+)/);
}
printf("%d\n",num);
}
}
}
return ;
}

pat1057. Stack (30)的更多相关文章

  1. PAT1057 stack(分块思想)

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

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

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

  3. pat 甲级 1057 Stack(30) (树状数组+二分)

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

  4. pat1057 stack

    超时算法,利用2的特殊性,用2个multiset来维护.单个multiset维护没法立即找到中位数. 其实也可以只用1个multiset,用一个中位指针,++,--来维护中位数. #include&l ...

  5. 1057. Stack (30)

    分析: 考察树状数组 + 二分, 注意以下几点: 1.题目除了正常的进栈和出栈操作外增加了获取中位数的操作, 获取中位数,我们有以下方法: (1):每次全部退栈,进行排序,太浪费时间,不可取. (2) ...

  6. PAT 1057. Stack (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057 用树状数组和二分搜索解决,对于这种对时间复杂度要求高的题目,用C的输入输出显然更好 #i ...

  7. PAT (Advanced Level) 1057. Stack (30)

    树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...

  8. 1057. Stack (30) - 树状数组

    题目如下: Stack is one of the most fundamental data structures, which is based on the principle of Last ...

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

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

随机推荐

  1. Java泛型读书笔记 (三)

    泛型对于老代码的支持 Java的泛型设计成类型擦除的目的,很大一部分是为了兼容老老代码.如下的一段代码: void setLabelTable(Dictionary table) table的类型是非 ...

  2. C# 使用SendMessage 函数

    在C#中,程序采用了的驱动采用了事件驱动而不是原来的消息驱动,虽然.net框架提供的事件已经十分丰富,但是在以前的系统中定义了丰富的消息对系统的编程提供了方便的实现方法,因此在C#中使用消息有时候还是 ...

  3. android studio中使用recyclerview小白篇(一)

    本人就是小白,昨天在使用listview时,看到说有更好的控件出来了,在V7包中,需要SDK21及以上,那就试着用用吧,今天试了一天,终于弄的能简单使用了,分享一下. 怎么导入这个recycleyvi ...

  4. Layer 父子页面之间的交互

    父页面获取子页面 var body = layer.getChildFrame('body',index);//建立父子联系 body.find("#parameter").val ...

  5. 树形DP--求树上任意两点间距离和

    例题:HDU2376   HDU6446(2018CCPC网络赛) 思路:求任意两点间距离和可以转换为->路径长度乘经过路径次数的和. 求经过次数:设这条边两端的点,被经过的次数分别为A和B,那 ...

  6. vtk-py求3d模型表面积

    模型格式:.obj 环境:python3.6+vtk7.1 vtk版: import vtk filename = "XXXX.obj" reader = vtk.vtkOBJRe ...

  7. java基础之转义符、数据类型

    一.  转义符 1.\n \n的作用是换行,也就是和键盘上的回车键相同 2.\t \t的作用是制表,就是以八个空格为一个单位,当不足八个时会自动补齐八个,如asd\tfgh,那么输出的将会是 . 3. ...

  8. 区块链中的密码学(四)- Merkle树和SPV节点

    什么是Merkle Tree? Merkle Tree 的命名来自于美国密码学家Ralph C. Merkle ,关于他的个人资料:传送门https://en.wikipedia.org/wiki/R ...

  9. django 基础框架学习 (二)

    Django框架基础-02 Django缓存cookie 1.说明        当我们服务器在响应数据的同时,希望写⼊⼀些缓存数据到客户端        我们可以选择在响应的同时,将要写⼊到客户端的 ...

  10. 平衡树学习笔记(5)-------SBT

    SBT 上一篇:平衡树学习笔记(4)-------替罪羊树 所谓SBT,就是Size Balanced Tree 它的速度很快,完全碾爆Treap,Splay等平衡树,而且代码简洁易懂 尤其是插入节点 ...