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 (-th smallest element if N is even, or (-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 (≤). 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 1.

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
思路:采用分块的思想,将数据分成数据范围的sqrt(N)块,上取整,每块中的元素个数不超过sqrt(N)下取整,block[i]记录每块含有的元素个数
table[x]记录元素出现的次数
 #include <iostream>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cmath> using namespace std ; const int N = ,K = ;
int block[K+] ;
int table[N] ; stack<int> stk ; int peekMedian(int cnt){
int sum = ;
if(cnt%){
cnt = (cnt+)/ ;
}else{
cnt /= ;
}
int i = ,idx = cnt ;
for(;i<K;i++){
sum += block[i] ;
if(sum>=cnt){
break ;
}
idx -= block[i] ;
}
for(int j=i*K;j<K*(K+);j++){
if(idx<=table[j]){
return j ;
}
idx -= table[j] ;
}
} int main(){
int n ;
cin >> n ; int cnt = ;
while(n--){
string cmd ;
cin >> cmd ; if(cmd == "Pop"){
if(stk.empty()){
cout << "Invalid" << endl ;
}else{
int ele = stk.top() ;
stk.pop() ;
cout << ele << endl ;
block[ele/K] -- ;
table[ele] -- ;
cnt -- ;
}
}else if(cmd == "Push"){
int x ;
cin >> x ;
stk.push(x) ;
block[x/K] ++ ;
table[x] ++ ;
cnt ++ ;
}else{
if(stk.empty()){
cout << "Invalid" << endl ;
}else{
cout << peekMedian(cnt) << endl ;
}
}
} return ; }

...

PAT1057 stack(分块思想)的更多相关文章

  1. Codeforces Round #319 (Div. 1)C. Points on Plane 分块思想

                                                                              C. Points on Plane On a pl ...

  2. ZOJ 1654 Place the Robots建图思维(分块思想)+二分匹配

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 AC一百道水题,不如AC一道难题来的舒服. 题意:一个n*m地图 ...

  3. HDOJ 4858 项目管理 ( 只是有点 莫队的分块思想在里面而已啦 )

    题目: 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858 题意: 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的! ...

  4. 莫队算法 sqrt(n)分块思想

    在此说一下本渣对莫队算法思想的一些浅薄理解 莫队算法的思想就是对真个区间的分块,然后按照每块来分别进行计算,这样最终的复杂度可以达到n*sqrt(n) 小Z的袜子是一道非常经典的题目.:题目链接htt ...

  5. PAT-1057 Stack (树状数组 + 二分查找)

    1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...

  6. pat1057 stack

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

  7. [BZOJ 2957]楼房重建(THU2013集训)(分块思想)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2957 分析: 首先明确问题,对于每栋楼房的斜率K=H/X,问题就是问有多少个楼房的K比前面所有 ...

  8. hdu_5110_Alexandra and COS(DP+分块思想)

    题目连接:hdu_5110_Alexandra and COS 题意: 给你一个图,X代表宝藏,然后有一个船,它的声纳的频率为D,定船到宝藏的距离为Dis=max(abs(x1-x2),abs(y1- ...

  9. hdu_5085_Counting problem(莫队分块思想)

    题目连接:hdu_5085_Counting problem 题意:给你一个计算公式,然后给你一个区间,问这个区间内满足条件的数有多少个 题解:由于这个公式比较特殊,具有可加性,我们考虑讲一个数分为两 ...

随机推荐

  1. 在CAD中插入谷歌地球卫星地图

    本文主要介绍如何在CAD中插入谷歌地球卫星地图,作为参照光栅图像.谷歌地球卫星地图使用“迈高图-地图数据下载器”(以下简称:迈高图)下载.迈高图会给出相关插入参数(插入点和缩放比例),保证插入卫星地图 ...

  2. golang微服务框架go-micro 入门笔记1.搭建 go-micro环境

    微服务的本质是让专业的人做专业的事情,做出更好的东西. golang具备高并发,静态编译等特性,在性能.安全等方面具备非常大的优势.go-micro是基于golang的微服务编程框架,go-micro ...

  3. Go ---- defer 和 return 执行的先后顺序

    Go 中 defer 和 return 执行的先后顺序 多个defer的执行顺序为“后进先出”: defer.return.返回值三者的执行逻辑应该是:return最先执行,return负责将结果写入 ...

  4. Spring Security的RBAC数据模型嵌入

    1.简介 ​ 基于角色的权限访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注.在RBAC中,权限与角色相关联,用户通过成 ...

  5. .Net MVC如何渲染带有网页标签的字符串

    有时候我们在解析一段文字时,可能文字中会包含网页上的标签,如div.p等等.那么如果将这种文字渲染成对应的标签效果呢?如图,最近博主就拿到了这么一段字符串(如图) 由于中间带有很多特殊字符,用Html ...

  6. 如何在CentOS / RHEL 7上启用IPv6

    默认情况下,在RHEL / CenOS 7系统上启用IPv6.因此,如果故意在系统上禁用IPv6,则可以通过以下任一方法重新启用它. 1.在内核模块中启用IPv6(需要重启)2.使用sysctl设置启 ...

  7. 2019 浪潮java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.浪潮等公司offer,岗位是Java后端开发,因为发展原因最终选择去了浪潮,入职一年时间了,之前面试了很多家公 ...

  8. JavaScript_proto_和prototype到底是什么玩意

    _proto_和prototype到底有什么区别啊?是个什么东西啊? 在这里我头也比较大啊,小学语文没学好,所以组织能力比较差劲,所以尽量的咱用代码来解释吧. function too() { thi ...

  9. HTML实用文本框样式

    输入框景背景透明: <input style="background:transparent;border:1px solid #ffffff"> 鼠标划过输入框,输入 ...

  10. Flask蓝图Blueprint和特殊装饰器

    Flask 中的 蓝图 Blueprint 不能被run的flask实例:相当于django中的app01 应用 蓝图作用:功能隔离 路由隔离 Blueprint就是 一个不能run的flask 蓝图 ...