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. c# winform使用IrisSkin2换肤弹不出窗口解决方案

    winform使用IrisSkin2换肤弹不出窗口解决方案 这张图能解决超级多博友的问题,知识是共享的,绝不私吞.共同学习,共同进步! 不需要说太多,只需1张图!                    ...

  2. Java打包多文件成zip

    package com.zh.java.util; import lombok.extern.slf4j.Slf4j; import java.io.File;import java.io.FileI ...

  3. UWP&WP8.1 基础控件—TextBlock和TextBox

    TextBlock:文本展示控件,有着强大的功能 TextBox:文本输入控件. 这两个控件是最为常用的基础控件. TextBlock 基础用法: 打开一个UWP项目,在XAML设计页面你可以从工具箱 ...

  4. fseek函数

    函数名:fseek函数 头文件:#include<stdio.h> 功能:把与fp有关的文件位置指针放到一个指定位置. 格式:  int fseek(FILE *stream, long ...

  5. P1891 疯狂LCM

    \(\color{#0066ff}{ 题目描述 }\) 众所周知,czmppppp是数学大神犇.一天,他给众蒟蒻们出了一道数论题,蒟蒻们都惊呆了... 给定正整数N,求LCM(1,N)+LCM(2,N ...

  6. typeof 和 instanceof

    typeof 和 instanceof 都是用来判断类型的函数 typeof 对于原始类型来说,除了 null 都可以显示正确的类型 typeof 1 // 'number' typeof '1' / ...

  7. linux线程切换和进程切换

    进程切换分两步: 1.切换页目录以使用新的地址空间 2.切换内核栈和硬件上下文 对于linux来说,线程和进程的最大区别就在于地址空间,对于线程切换,第1步是不需要做的,第2是进程和线程切换都要做的. ...

  8. [USACO09FEB]改造路Revamping Trails 分层最短路 Dijkstra BZOJ 1579

    题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...

  9. opencv-视频基本操作

    写视频 # encoding: utf-8 ''' @author: gaoyongxian666 @file: opencv_video_write.py @time: 2018/4/15 11:1 ...

  10. DJ

    必知必会13条 all():查询所有结果 filter(**kwargs)   过滤,取到符合条件的对象,比get方法好,找不到会返回空 get(**kwargs)      取到符合条件对象,对象有 ...