PAT1057 stack(分块思想)
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
思路:采用分块的思想,将数据分成数据范围的sqrt(N)块,上取整,每块中的元素个数不超过sqrt(N)下取整,block[i]记录每块含有的元素个数
Invalid
3
2
2
1
2
4
4
5
3
Invalid
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(分块思想)的更多相关文章
- Codeforces Round #319 (Div. 1)C. Points on Plane 分块思想
C. Points on Plane On a pl ...
- ZOJ 1654 Place the Robots建图思维(分块思想)+二分匹配
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 AC一百道水题,不如AC一道难题来的舒服. 题意:一个n*m地图 ...
- HDOJ 4858 项目管理 ( 只是有点 莫队的分块思想在里面而已啦 )
题目: 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858 题意: 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的! ...
- 莫队算法 sqrt(n)分块思想
在此说一下本渣对莫队算法思想的一些浅薄理解 莫队算法的思想就是对真个区间的分块,然后按照每块来分别进行计算,这样最终的复杂度可以达到n*sqrt(n) 小Z的袜子是一道非常经典的题目.:题目链接htt ...
- PAT-1057 Stack (树状数组 + 二分查找)
1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...
- pat1057 stack
超时算法,利用2的特殊性,用2个multiset来维护.单个multiset维护没法立即找到中位数. 其实也可以只用1个multiset,用一个中位指针,++,--来维护中位数. #include&l ...
- [BZOJ 2957]楼房重建(THU2013集训)(分块思想)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2957 分析: 首先明确问题,对于每栋楼房的斜率K=H/X,问题就是问有多少个楼房的K比前面所有 ...
- hdu_5110_Alexandra and COS(DP+分块思想)
题目连接:hdu_5110_Alexandra and COS 题意: 给你一个图,X代表宝藏,然后有一个船,它的声纳的频率为D,定船到宝藏的距离为Dis=max(abs(x1-x2),abs(y1- ...
- hdu_5085_Counting problem(莫队分块思想)
题目连接:hdu_5085_Counting problem 题意:给你一个计算公式,然后给你一个区间,问这个区间内满足条件的数有多少个 题解:由于这个公式比较特殊,具有可加性,我们考虑讲一个数分为两 ...
随机推荐
- 图论 --- 三维空间bfs
<传送门> [题目大意] 给你一个三维的迷宫,让你计算从入口走到出口最少步数. [题目分析] 其实和二维迷宫还是一样的,还是用队列来做,由于BFS算法一般是不需要回溯的,所以我们就用不着还 ...
- jdk的下载路径和环境变量的配置
一:jdk百度网盘的下载路径: 链接:https://pan.baidu.com/s/1pF41oGcTqouULsWKEBn3hw 提取码:p1y2 复制这段内容后打开百度网盘手机App,操作更方便 ...
- STVD使用printf输出数据错误
使用STM8L052输出调试信息 重定向put char #include "stdio.h" //必不可缺少 char putchar (char c) { /* Write a ...
- Scala 安装 Scala for Eclipse安装及运行hello word
Scala下载安装地址:https://www.scala-lang.org/download/ .windows版本的安装包是scala-2.12.8.msi.直接滑动到网页最下面,下载对应的系统的 ...
- mpx小程序框架
在构建自己mpx小程序demo的时候遇到的问题 1.关于自定义tabbar的问题 1.1 在根据微信小程序的自定义tabbar来做 ▲在与src同级目录创建 custom-tab-bar 文件夹 创建 ...
- Python调用Matlab2014b引擎
用惯Python的你,是不是早已无法忍受matplotlib那丑陋无比的图以及蛋疼无比部署依赖? 当当当当,Matlab2014b的Python Engine API现已加入豪华午餐. 上次写了一篇文 ...
- jquery获取form表单中的数据
$(function() { $('#submit').click(function() { var d = {}; var t = $('form').serializeArray(); //t的值 ...
- Spring Data Jpa 复杂查询总结
实体类 @Entity @Table(name = "t_hotel") @Data public class THotel { @Id private int id; priva ...
- java-java技术链接
java基础知识总结大全:https://blog.csdn.net/hao19980724/article/details/83792516 Java核心技术梳理-集合:https://mp.wei ...
- js 数组 深拷贝 复制 (汇总)
https://www.cnblogs.com/zhoupengyi/p/6048955.html https://www.cnblogs.com/racyily/p/3532176.html htt ...