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 题意:给你一个计算公式,然后给你一个区间,问这个区间内满足条件的数有多少个 题解:由于这个公式比较特殊,具有可加性,我们考虑讲一个数分为两 ...
随机推荐
- Python学习之路:关于列表(List)复制的那点事
要谈列表的复制,我们就要谈到Python的赋值规则 首先我们创建列表a: a = [1,2,3] 通常我们复制一个元素的方法是这样的: b = a #复制元素的一般方法 print(a) print( ...
- Delphi百度文字识别【支持通用文字识别、身份证识别、银行卡识别、驾驶证识别、行驶证识别、车牌识别等功能】
作者QQ:(648437169) 点击下载➨Delphi百度文字识别 百度api文档 [Delphi百度文字识别]支持 通用文字识别.通用文字识别(高精度版).通用文字识别(含位置信 ...
- Linux下嵌入式Web服务器BOA和CGI编程开发
**目录**一.环境搭建二.相关配置(部分)三.调试运行四.测试源码参考五.常见错误六.扩展(CCGI,SQLite) # 一.环境搭建操作系统:Ubuntu12.04 LTSboa下载地址(但是我找 ...
- Jenkins服务使用 宿主机的docker、docker-compose (Jenkins 执行sudo命令时出现“sudo: no tty present and no askpass program specified”,以及 docker-compose command not found解决办法)
若要转载本文,请务必声明出处:https://www.cnblogs.com/zhongyuanzhao000/p/11681474.html 原因: 本人最近正在尝试CI/CD,所以就使用了 Jen ...
- Shell编程学习(七)
if 条件语句的知识与实践 if 条件语句 if条件语句的语法 单分支结构 第一种 if <条件测试表达式> then 指令 fi 第二种 if <条件测试表达式>; then ...
- 上传docker镜像到阿里云镜像源
阿里云docker镜像配置 阿里云用户名可以使用淘宝系的,或者新注册都行. a. 配置阿里云的镜像加速器:加速器 然后在线上创建`镜像仓库`,需要设置`命名空间`和`仓库名称`,然后接着操作下面的步骤 ...
- 阿里巴巴 Java 开发手册 (九) 异常日志
(一) 异常处理 1. [强制]Java 类库中定义的一类 RuntimeException 可以通过预先检查进行规避,而不应该 通过 catch 来处理,比如:IndexOutOfBoundsExc ...
- sqlserver还原差异备份
因为之前遇到还原差异备份,最开始遇到SQLServer报错:"无法还原日志备份或差异备份,因为没有文件可用于前滚".查阅很多资料后,终于得到解决.收集整理成这篇随笔. 问题原因:出 ...
- igel udc2 config
igel udc2 config 系统安装盘下载地址 http://www.myigel.biz/?forcedownload /config/bin/igelone_config #!/bin/sh ...
- Django:信号的使用
信号 Django中提供了"信号调度",用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. 应用:比如插入数据到数据库,插入之前写日 ...