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的更多相关文章

  1. PAT甲级——A1057 Stack

    Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...

  2. 线性数据结构之栈——Stack

    Linear data structures linear structures can be thought of as having two ends, whose items are order ...

  3. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  4. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  5. Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder

    Stack Overflow 排错翻译  - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...

  6. Uncaught RangeError: Maximum call stack size exceeded 调试日记

    异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...

  7. Stack操作,栈的操作。

    栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...

  8. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  9. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

随机推荐

  1. Eclipse的智能提示的设置

    智能提示修改方式是: Windows——>Preferences——>Java-->Editor-->Content Asist,在Auto activation trigge ...

  2. Django--CRM--菜单展示, 删除合并, 权限展示

    一 . 菜单展示 二 . 合并删除 我们可以把所有的删除都合并成一个函数这样就会减少很多的代码. 思路: 在url里面需要传两个参数,一个是要删的id 一个是名字 三 .权限展示 我们要实现两个功能 ...

  3. sonar结合jenkins

    一.下载jenkins插件 二.系统设置 三.获取token值 4.调整 Jenkins 构建设置

  4. LeetCode & Online Programming Learning Platform

    leetcode LeetCode is the best platform to help you enhance your skills, expand your knowledge and pr ...

  5. $.ajax的async设置true和false的区别一点笔记

    async的默认值是true 当async为true时,为异步请求 如果一个$.ajax的函数在另一个函数中调用,不一定会等该函数调用完再加载完函数 导致产生空值的问题 而在JS函数中调用$.ajax ...

  6. Delphi处理数据网格DBGrid的编辑框 获取还没有提交到数据集的字段文本

    //fromhttp://kingron.myetang.com/zsfunc12.htm (*//标题:处理数据网格的编辑框说明:示例添加焦点颜色;获取还没有提交到数据集的字段文本设计:Zswang ...

  7. Bootstrap之信息记录

    Bootstrap中文网: http://www.bootcss.com/ 上面有一些资料和范例 实例精选: https://v3.bootcss.com/getting-started/#examp ...

  8. 四、K8S

    一.查看日志 journalctl -xeu kubelet

  9. 扩展运算符(spread)是三个点(…)

    扩展运算符(spread)是三个点(…),将一个数组||类数组||字符串转为用逗号分隔的序列. js中用来对数组进行操作,把数组里面的东西统统拿出来 一.展开数组 //展开数组 let a = [1, ...

  10. hdu-4763(kmp+拓展kmp)

    题意:给你一个串,问你满足最大字串既是前后缀,也在字符串除去前后缀的位置中出现过: 思路:我用的是拓展kmp求的前后缀,只用kmp也能解,在字符串2/3的位置后开始遍历,如果用一个maxx保存前2/3 ...