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. 50分钟学会Laravel 50个小技巧(基于laravel5.2,仅供参考)

    转载请注明:转载自 Yuansir-web菜鸟 | LAMP学习笔记 本文链接地址: 50分钟学会Laravel 50个小技巧 原文链接:< 50 Laravel Tricks in 50 Mi ...

  2. 读懂掌握 Python logging 模块源码 (附带一些 example)

    搜了一下自己的 Blog 一直缺乏一篇 Python logging 模块的深度使用的文章.其实这个模块非常常用,也有非常多的滥用.所以看看源码来详细记录一篇属于 logging 模块的文章. 整个 ...

  3. Selenium简单回顾

    一.Selenium介绍 1.Selenium(浏览器自动化测试框架): Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的 ...

  4. servletContext和request对象的生命周期比较

    ServletContext: 创建:服务器启动 销毁:服务器关闭 域的作用范围:整个web应用 Request: 创建:访问时创建request 销毁:响应结束request销毁 域的作用范围:一次 ...

  5. Windows 7 SP1 x64 LSP

    NALapi.dll napinsp.dll pnrpnsp.dll mswsock.dll winrnr.dll

  6. C# 获取文件详细备注信息 (如图片、视频实际创建时间)

    在整理照片/视频时想根据实际拍摄时间重命名文件,但 System.IO.FileInfo 只能获取到文件的创建时间或最后写入时间,不符合要求,遂寻找解决方案 方案 1: System.Drawing ...

  7. C#程序中设置全局代理(Global Proxy)

    1. HttpWebRequest类的Proxy属性,只要设置了该属性就能够使用代理了,如下: 1             //设置代理 2         WebProxy WP = new Web ...

  8. Multi-Targeting and Porting a .NET Library to .NET Core 2.0

    Creating a new .NET Standard Project The first step for moving this library is to create a new .NET ...

  9. 前后端进行数据交互时候 要优先考虑json格式即简直对形式,[{}] 列表形式 等便于操作的数据结构

    前后端进行数据交互时候 要优先考虑json格式即简直对形式,[{}] 列表形式 等便于操作的数据结构

  10. 洛谷 P1538 迎春舞会之数字舞蹈

    题目背景 HNSDFZ的同学们为了庆祝春节,准备排练一场舞会. 题目描述 在越来越讲究合作的时代,人们注意的更多的不是个人物的舞姿,而是集体的排列. 为了配合每年的倒计时,同学们决定排出——“数字舞蹈 ...