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打开java文件繁体字

    右键-->properties-->Resource-->Text file encoding, 改成utf-8 .

  2. 批量处理word所有回车行

    在WORD中点击CTRL+H,弹出对话框,输入如下替换符

  3. C# Note30: 网络爬虫

    用C#实现网络爬虫(一) 用C#实现网络爬虫(二) 基于C#.NET的高端智能化网络爬虫(一)(反爬虫哥必看) 基于C#.NET的高端智能化网络爬虫(二)(攻破携程网) C#获取网页内容的三种方式

  4. C# Note14: Editable WPF ListView

    (1)https://stackoverflow.com/questions/5652527/editable-wpf-listview (2)How to: Create a ListView wi ...

  5. flutter-StatelessWidget与StatefulWidget

    StatelessWidget和StatefulWidget是flutter的基础组件,日常开发中自定义Widget都是选择继承这两者之一. 两者的区别在于状态的改变,StatelessWidget面 ...

  6. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  7. pip install MySQL-python 失败

    1. EnvironmentError: mysql_config not found原因:/usr/bin/mysql_config没有次文件,要安装libmysqlclient-dev, apt ...

  8. crontab注意%

    %在其中有特殊含义表示开始新行 十分坑 例子:写一个定时任务用到date命令 crontab -e * * * * * date +%F >> /tmp/time.log 查看我们的cro ...

  9. How the Microsoft Bot Framework Changed Where My Friends and I Eat: Part 1

    Bots are everywhere nowadays, and we interact with them all of the time. From interactions on our ph ...

  10. 前端传递给后端且通过cookie方式,尽量传递id

    前端传递给后端且通过cookie方式,尽量传递id