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. Hyperledger Fabric 入门 first-network 搭建

    1.准备环境: 安装git.docker.curl.go [root@test_vonedao_83 fabric]# git --version git version 1.8.3.1 [root@ ...

  2. 第2课,python while循环的使用

    引言: 上次课学习了python turtle库的基本使用,向前向后和转向.本次课需要画多个图形,简单的东西多起来就变得不简单了. 0/1是简单的,但却能组成丰富多彩的多媒体世界. 课程内容: 1. ...

  3. (十)pdf的构成之交叉引用表

    交叉引用表(xref) 其中包含对文档中所有对象的引用.交叉引用表的目的是允许随机访问文件中的对象,因此我们不需要读取整个PDF文档来定位特定对象.每个对象由交叉​​引用表中的一个条目表示.(该表保存 ...

  4. 删除elasticsearch 30天前的所有索引

    我的索引格式为 xxx-xxx-xxx-2019.06.27 xxx-xxxx-2019.06.27 脚本思路: 获取目前 es上所有索引,以日期进行拆分,然后用索引时间对比一个月前的日期,日期小于一 ...

  5. linux tomcat开机自启/nginx开机自启

    修改/etc/rc.d/rc.local文件,修改完成后需执行以下指令才能正常自启动 chmod +x /etc/rc.d/rc.local #!/bin/bash # THIS FILE IS AD ...

  6. Spring mvc请求处理流程详解(一)之视图解析

      本文链接:https://blog.csdn.net/lchpersonal521/article/details/53112728 前言 Spring mvc框架相信很多人都很熟悉了,关于这方面 ...

  7. 使用Powershell实现自动化安装/卸载程序

    最近需要制作软件安装包,需要附带VC运行时和.Net Framework的安装,但又不想让用户自己点下一步,所以就有了以下操作. 微软提供了一个程序叫msiexec.exe,位于C:\Windows\ ...

  8. 【开发笔记】- 在Windows环境下后台启动redis

    1. 进入 DOS窗口 2. 在进入Redis的安装目录 3. 输入:redis-server --service-install redis.windows.conf --loglevel verb ...

  9. nodeJS从入门到进阶三(MongoDB数据库)

    一.MongoDB数据库 1.概念 数据库(DataBase)是一个按照数据结构进行数据的组织,管理,存放数据的仓库. 2.关系型数据库 按照关系模型存储的数据库,数据与数据之间的关系非常密切,可以实 ...

  10. js数组(五)

    一.数组的声明方法一:[构造函数形式声明] var array = new Array(); //[] var array = new Array(10); //数组长度:array.length = ...