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 ;
}

最新文章

  1. RegExp对象
  2. 第三天 moyax
  3. Content is not allowed in prolog.解决方法
  4. sql如何将同个字段不同值打印在一行
  5. VI编辑器学习笔记
  6. NOIP201305转圈游戏
  7. Debian5.04安装oracle11g 笔记
  8. 获取属性名:PropertyNameHelper
  9. C# .NET 获取枚举值的自定义属性(特性/注释/备注)信息
  10. Android开发技巧——ViewPager衍生出来的2个类
  11. linux 安装配置 sublime 进行 python 开发
  12. JDK1.7和JDK1.8对于异常的支持
  13. windows类书的学习心得
  14. Chrome中Vim插件cVim
  15. Eclipse (indigo) 中安装jdk包并执行Maven
  16. 洛谷p1067
  17. 泡泡一分钟:Towards real-time unsupervised monocular depth estimation on CPU
  18. document的全量替换、强制创建、删除
  19. 回归JavaScript基础(四)
  20. 卡特兰数Catalan——定义、公式、模型总结

热门文章

  1. Dreamweaver Flash Photoshop网页设计综合应用 (智云科技) [iso] 1.86G​
  2. C#利用phantomJS抓取AjAX动态页面
  3. C#内存映射大文件并使用Marshal解析结构体信息
  4. 20164305 徐广皓 Exp6 信息搜集与漏洞扫描
  5. Elaxia的路线
  6. Beautiful Sequence
  7. django 学习之DRF (一)
  8. DB2存储过程标准
  9. javascript事件委托//就是父级事件给子级
  10. 001 开发环境搭建、安卓项目结构、R文件位置、asset目录创建