A1057. 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 (N/2)-th smallest element if N is even, or ((N+1)/2)-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 (<= 105). 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 105.
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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<math.h>
using namespace std;
int hashTB[] = {}, block[] = {};
stack<int> stk;
int main(){
int N, num;
int bk = sqrt(100000.0);
string ss;
cin >> N;
for(int i = ; i < N; i++){
cin >> ss;
if(ss == "Pop"){
if(stk.empty() == false){
num = stk.top();
cout << num << endl;
stk.pop();
hashTB[num]--;
block[num / bk]--;
}else{
cout << "Invalid" << endl;
}
}else if(ss == "Push"){
cin >> num;
stk.push(num);
hashTB[num]++;
block[num / bk]++;
}else if(ss == "PeekMedian"){
if(stk.empty() == true){
cout << "Invalid" << endl;
}else{
int cnt = stk.size(), j = , sum = ;
if(cnt % == )
cnt = cnt / ;
else cnt = (cnt + ) / ;
while(sum < cnt && j < bk){
sum += block[j];
j++;
}
j--;
sum = sum - block[j];
for(j = j * bk; j < ; j++){
sum += hashTB[j];
if(sum >= cnt){
cout << j << endl;
break;
}
}
}
}
}
return ;
}
总结:
1、超时了三个点,先这样吧,以后有时间再想想。用一个stack来同步模拟题中的push与pop操作。另使用一个hash表维护每个元素出现的次数。需要中位数时,就从头开始累加次数,直到达到中位数为止。为了加快速度,还可以将hash表分成N块,相当于建立一个索引表,存储每一块块内的元素个数。这样就可以先搜索块,次数差不多之后,再进入该块内搜索。分块的下标、循环条件等等不好想时,先简化成只有2块,每块50个元素来类比。
2、记得每次提交都要把最后的cin去掉。
更新: 把cin cout string 换成 scanf printf 和 char [ ] 竟然全过了。看来还是能不用cin cout就不用啊。下面是最新的代码。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<math.h>
using namespace std;
int hashTB[] = {}, block[] = {};
stack<int> stk;
int main(){
int N, num;
int bk = sqrt(100000.0);
char ss[];
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%s", ss);
if(ss[] == 'o'){
if(stk.empty() == false){
num = stk.top();
printf("%d\n", num);
stk.pop();
hashTB[num]--;
block[num / bk]--;
}else{
printf("Invalid\n");
}
}else if(ss[] == 'u'){
scanf("%d", &num);
stk.push(num);
hashTB[num]++;
block[num / bk]++;
}else if(ss[] == 'e'){
if(stk.empty() == true){
printf("Invalid\n");
}else{
int cnt = stk.size(), j = , sum = ;
if(cnt % == )
cnt = cnt / ;
else cnt = (cnt + ) / ;
while(sum < cnt && j < bk){
sum += block[j];
j++;
}
j--;
sum = sum - block[j];
for(j = j * bk; j < ; j++){
sum += hashTB[j];
if(sum >= cnt){
printf("%d\n", j);
break;
}
}
}
}
}
cin >> num;
return ;
}
A1057. Stack的更多相关文章
- PAT甲级——A1057 Stack
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- 线性数据结构之栈——Stack
Linear data structures linear structures can be thought of as having two ends, whose items are order ...
- Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)
--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder
Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...
- Uncaught RangeError: Maximum call stack size exceeded 调试日记
异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...
- Stack操作,栈的操作。
栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
随机推荐
- JS中的<a>标签
<a>标签可定义锚.一个锚有两种用法: 通过使用 href 属性,创建一个到另外一个文档的链接 通过使用 name 或 id 属性,创建一个文档内部的书签 如果是在 HTML 5 中,它定 ...
- Kafka-Flume-elasticsearch
a1.sources = kafkaSource a1.channels = memoryChannel a1.sinks = elasticsearch a1.sources.kafkaSource ...
- ubuntu18.04 安装 php7.2
sudo apt-get install software-properties-common python-software-properties sudo add-apt-repository p ...
- Fiddler-学习笔记-远程抓包
1 操作系统低于win7用 fiddler 2 win7 或win7以上版本,用 fiddler4片本 2 fiddler开关:左下角或点击F12控件fiddler开关,开=capturing 3 启 ...
- Sql Server 时间格式化
0 或 100 (*) 默认值 mon dd yyyy hh:miAM(或 PM) 1 101 美国 mm/dd/yyyy ...
- Nginx 网络事件
L27-29 应用层(如浏览器等一系列组成的发送get请求) 传输层 系统内核打开一个端口将客户端IP及端口和服务端IP及端口记录下来一并传输到网络层 网络层 打包后到链路层 再到客户端路由器至广域网
- redis日常使用汇总--持续更新
redis日常使用汇总--持续更新 工作中有较多用到redis的场景,尤其是触及性能优化的方面,传统的缓存策略在处理持久化和多服务间数据共享的问题总是不尽人意,此时引入redis,但redis是单线程 ...
- VSCode里面HTML添加CSS时没有提示
看到知乎上的回答,vscode修改设置的: "editor.parameterHints": true, "editor.quickSuggestions": ...
- BZOJ2176Strange string——最小表示法
题目描述 给定一个字符串S = {S1, S2, S3 … Sn}, 如果在串SS中, 子串T(|T| = n)为所有长度为n的SS的字串中最小的(字符串的比较), 则称T为”奇怪的字串”. 你的任务 ...
- Redis——redis使用redis-dump,redis-load导出导入数据——【三】
来源 https://www.cnblogs.com/dadonggg/p/8662455.html https://blog.csdn.net/chenxinchongcn/article/deta ...