pat1057. Stack (30)
1057. Stack (30)
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)的更多相关文章
- PAT1057 stack(分块思想)
1057 Stack (30分) Stack is one of the most fundamental data structures, which is based on the princ ...
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
- pat 甲级 1057 Stack(30) (树状数组+二分)
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...
- pat1057 stack
超时算法,利用2的特殊性,用2个multiset来维护.单个multiset维护没法立即找到中位数. 其实也可以只用1个multiset,用一个中位指针,++,--来维护中位数. #include&l ...
- 1057. Stack (30)
分析: 考察树状数组 + 二分, 注意以下几点: 1.题目除了正常的进栈和出栈操作外增加了获取中位数的操作, 获取中位数,我们有以下方法: (1):每次全部退栈,进行排序,太浪费时间,不可取. (2) ...
- PAT 1057. Stack (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057 用树状数组和二分搜索解决,对于这种对时间复杂度要求高的题目,用C的输入输出显然更好 #i ...
- PAT (Advanced Level) 1057. Stack (30)
树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
- 1057. Stack (30) - 树状数组
题目如下: Stack is one of the most fundamental data structures, which is based on the principle of Last ...
- PAT甲级题解-1057. Stack (30)-树状数组
不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...
随机推荐
- C#中关于换行符的记录
最近在做一个练习的时候,从其他数据库提出来数据装到自己的数据表中,发现同是编辑器的内容却在页面上显示不出来,但是在数据库中又确实存在,经过一番折腾之后发现是 换行符 的问题.在我的编辑器中是以 ‘\r ...
- 泛型List<T>转存为XML文档
经常会有这情况,在程序处理结果为泛型List<T>,但为了能把这些集合输出XML文档.Insus.NET就因此问题,演示一个范例.在程序中,创建一个类: List<T>的集合手 ...
- linux通配符与基础正则
1.特殊符号: ' ' 单引号 单引号的内容 写什么就是什么,不会被当成特殊字符. 单引号实例: [root@oldboyedu-sh01 ~] ...
- Django之QuerySet 创建对象
在前面的模型介绍中设置了3个对象,出版商(publisher),作者(Authro),书籍(book).首先我们在网页中添加各个对象信息填写的界面.填写后点击提交.将会传递给后端.传递方式采用post ...
- 转载JAVA八大经典书籍,你看过几本?
一.Java从入门到精通*<Java从入门到精通(第3版)>从初学者角度出发,通过通俗易懂的语言.丰富多彩的实例,详细介绍了使用Java语言进行程序开发需要掌握的知识.<Java从入 ...
- C#中工厂模式的作用
1.比如,主要用于对扩展性有要求的功能. 以简单工厂为例: 接口Fun有三个实现 class FunA FunB FunC工厂 class Fac { public static Fun getF ...
- Mysql(Linux服务器)root用户密码忘记重置方法
MySQL是非常常见的开源数据库,使用者众多,若是不小心忘记了安装在服务器的mysql密码,无法登陆,应该如何重置呢?方法很简单,现在和大家分享下.(系统环境:CentOs 6.5 软件:Mysql ...
- Generic detail view PostDetailView must be called with either an object pk or a slug.解决
Django 使用DetailView有这个问题,url,和模板统一调用模型时,用pk,而不是id 如果不是用DetailView,只是简单的视图,则用pk 或者id都可以. urls.py: url ...
- [LOJ2027] [SHOI2016] 黑暗前的幻想乡
题目链接 LOJ:https://loj.ac/problem/2027 洛谷:https://www.luogu.org/problemnew/show/P4336 Solution 这题很像[ZJ ...
- flex item的width VS flex-basis
flexbox的子元素flex item的宽度,按照以下规则计算: content>width>flex-basis(limited by max/min-width) flex-basi ...