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 ...
随机推荐
- 判断浏览器是否支持H5
window.onload = function() { if (!window.applicationCache) { alert("请升级您的浏览器版本,你的浏览器不支持HTML5!&q ...
- Spring 基于XML配置
基于XML的配置 对于基于XML的配置,Spring 1.0的配置文件采用DTD格式,Spring2.0以后采用Schema格式,后者让不同类型的配罝拥有了自己的命名空间,使得配置文件更具扩展性.此外 ...
- laravel5.3安装redis扩展包
1,编辑 laravel 根目录下的 composer.json 文件: "require": { "php": ">=5.6.4", ...
- checkbox选中事件的正确写法
判断选中CHECKBOX事件 网上各种扯淡 搞死我了..加上总觉得smarty引擎和JSJQ有很多冲突.. $("#id").is(":checked");
- elasticsearch介绍,安装,安装错误解决及相应插件安装
一.elasticsearch介绍 1.简介(使用的是nosql,更新比mongodb慢): ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎, ...
- ps -aux返回超时的可能原因
在我的环境上出现了 ps -aux返回超时的问题. 执行到 ……root 19342 0.0 0.0 0 0 ? S< Mar12 0:00 [kworker/34:1H] 这里,然后就卡住了. ...
- 当应用程序不是以UserInteractive 模式运行时显示模式对话框或窗体
最近在做一个WCF程序的时候,WCF程序老是弹出一个错误“当应用程序不是以UserInteractive 模式运行时显示模式对话框或窗体是无效操作.请指定ServiceNotification或Def ...
- 使用synchronized 实现ReentrantLock(美团面试题目)
刚看到这个题目的时候无从下手,因为觉得synchronized和lock在加锁的方式上有很大不同,比如,看看正常情况下synchronized时如何加锁的. 方式一: public synchroni ...
- win10 新建文件夹没有了
1运行-regedit 2 在打开的注册表编辑器窗口,展开HKEY_CLASSES_ROOT,在HKEY_CLASSES_ROOT展开项中找到:Directory,再依次展开:Directory\Ba ...
- LODOP安装参数 及静默安装
在cmd命令里里静默安装lodop(c-lodop不能静默安装),本人的安装文件放在D:\lodopdownload\3060\Lodop6.224_Clodop3.060,如下所示: lodop静默 ...