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 ; }
...
最新文章
- NSLog 占位符
- NSBundle
- [转]LUA C 互调
- Github上LeakCanary编译报错CreateProcess error=2的解决方法
- 常见设计模式解析和实现(C++)FlyWeight模式
- EL标签库
- [Delphi]检查URL是否有效的函数
- C++中的类继承(2)派生类的默认成员函数
- org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression
- CentOS7 中文man(cman)配置方法
- js------科学计数法转换为正常小数
- 【JavaScript】获取当前页的URL与window.location.href
- javascript鼠标滚动
- web项目加载图片资源
- Codeforces 101173 C - Convex Contour
- 【git】之clone(克隆)
- C# winform 编程 向ACCESS数据库导入EXCEL表使用心得
- 几个经典的数学库之一学习---VCGlib(2)
- 基本控件文档-UISlider属性
- CCF - 最大矩形
热门文章
- C++实现base64编解码
- cf 595 补题
- CF241E Flights 差分约束
- MySQL8.0 下载安装启动(Windows10)
- Python进阶----进程之间通信(互斥锁,队列(参数:timeout和block),), ***生产消费者模型
- Flutter Platform Channels
- Js判断对象是否是数组的方法
- float与position间的区别
- 原生JavaScript实现轮播图
- 深入理解JVM-java内存区域与内存溢出异常