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 (-th smallest element if N is even, or (-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 (≤). 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 1.

Output Specification:

For each Push command, insert key into the stack and output nothing. For each Pop or PeekMediancommand, print in a line the corresponding returned value. If the command is invalid, print Invalidinstead.

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

数组数据是随时都在进行输入与删除的,让你实时反馈数组中的中位数:
使用分块思想
1、一般来说,为了达到高效率的目的,对一个有N个元素的有序序列来说,除最后一块外,其余每块中元素的个数都应当为 | √N | (此处为向下取整,方便程序实现),于是块数为[√N](此处为向上取整)。这样就把有序序列划分为[√N]块,其中每块中元素的个数不超过 | √N | 。
考虑到序列中的元素都是不超过105的非负整数,因此不妨设置一个hash数组table[100001],其中table[x]表示整数x的当前存在个数;
接着,借助分块思想,从逻辑上将0~10分为 | √(105 + 1) |= 317块,其中每块的元素个数为316。逻辑上进行分块的结果如下:
0,1,2.…,314,315为第0块;
316,317…,630,631为第1块。
99856,99857,…,100000为第316块。
这样分块有什么用呢?可以定义一个统计数组block[317],其中block[i]表示第i块中存在的元素个数。于是假如要新增一个元素x,就可以先计算出x所在的块号为x / 316,然后让block[x / 316]加1,表示该块中元素个数多了1;同时令table[x]加1,表示整数x的当前存
在个数多了1。
例如想要新增334这个元素,就可以通过334 / 316 = 1算出元素334所在的块号为1,然后令block[1]++,表示1号块增加了一个元素,并令table[334] + 1,表示元素334的存在个数多了1。
同理,如果想要删除一个元素x,只需要让block[x / 316]和table[x]都减1即可。显然,新增与删除元素的时间复杂度都是O(1)。
接着来看如何查询序列中第K大的元素是什么。
首先,从小到大枚举块号,利用block数组累加得到前i - 1块中存在的元素总个数,然后判断加入i号块的元素个数后元素总个数能否达到K。如果能,则说明第K大的数就在当前枚举的这个块中,此时只需从小到大遍历该块中的每个元素(其中i号块的第一个元素是i * 316),利用table数组继续累加元素的存在个数,直到总累计数达到K,则说明找到了序列第K大的数。显然整体思路是先用O(√N)的时间复杂度找到第K大的元素在哪一块,然后再用0(√N)的时间复杂度在块内找到这个元素,因此单次查询的总时间复杂度为0(√N)。

 #include <iostream>
#include <stack>
#include <string>
#include <cmath>
using namespace std;
int N, num, table[], block[];//数的个数,块中数的个数
int main()
{
cin >> N;
stack<int>s;
string str;
for (int i = ; i < N; ++i)
{
cin >> str;
if (str == "Pop")
{
if (s.size() > )
{
cout << s.top() << endl;
table[s.top()]--;
block[s.top() / ]--;
s.pop();
}
else
cout << "Invalid" << endl;
}
else if (str == "Push")
{
cin >> num;
s.push(num);
table[num]++;
block[num / ]++;
}
else
{
if (s.size() > )
{
int mid = s.size() % == ? s.size() / : (s.size() + ) / ;
int t = , k = ;
while (k + block[t] < mid) k += block[t++];
int num = t * ;
while (k + table[num] < mid)k += table[num++];
cout << num << endl;
}
else
cout << "Invalid" << endl;
}
}
return ;
}

PAT甲级——A1057 Stack的更多相关文章

  1. PAT甲级1057. Stack

    PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...

  2. PAT甲级1057 Stack【树状数组】【二分】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...

  3. PAT 甲级 1057 Stack

    https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 Stack is one of the mo ...

  4. PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

    1057 Stack (30 分)   Stack is one of the most fundamental data structures, which is based on the prin ...

  5. pat 甲级 1057 Stack(30) (树状数组+二分)

    1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...

  6. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  7. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  8. PAT甲级考前整理(2019年3月备考)之二,持续更新中.....

    PAT甲级考前整理之一网址:https://www.cnblogs.com/jlyg/p/7525244.html,主要总结了前面131题的类型以及易错题及坑点. PAT甲级考前整理三网址:https ...

  9. PAT甲级专题|树的遍历

    PAT甲级专题-树的遍历 涉及知识点:树.建树.深度优先搜索.广度优先搜索.递归 甲级PTA 1004 输出每一层的结点,邻接表vector建树后.用dfs.bfs都可以边搜边存当前层的数据, #in ...

随机推荐

  1. 快速搭建Bootstrap

    粘贴下面代码,快速开启Bootstrap的搭建: <!DOCTYPE html> <html lang="en"> <head> <met ...

  2. <jquery>滚动例子

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. java_缓冲流(字节输出流)

    缓冲流分为: 字节缓冲流:BufferedIntputSream(字节缓冲输出流),BufferdOutputStream(字节缓冲输入流) 字符缓冲流:BufferedReader(字符输入缓冲流) ...

  4. PHP网络请求优化

    目录 1. 设置超时时间 2. 将串行请求并行化 1. 设置超时时间 连接超时:200ms 读超时: 800ms 写超时: 500ms 2. 将串行请求并行化 使用curl_multi_*() 使用s ...

  5. LoadRunner如何调用外部函数

    LoadRunner如何调用外部函数 使用 VuGen 时,可以调用在外部 DLL 中定义的函数.通过从脚本调用外部函数,可以降低脚本的内存使用量以及总体运行时间.要调用外部函数,需要加载定义了该函数 ...

  6. 2016.8.17上午纪中初中部NOIP普及组比赛

    2016.8.17上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1335 本来觉得自己能考高分,但只得160分,并列第九.至少又挤 ...

  7. Centos--swoole平滑重启服务

    平滑重启: 已经打开的服务: 首先在server服务中为进程添加名字: /** * @param $server */ public function onStart($server) { swool ...

  8. 通过Amazon AWS 十分钟搭建私人主机 自由的不要不要的

      首先承认有点标题党了,当时自己搞的时候可不止十分钟,好吧,我承认是坑太多了,所以特意开了一篇博文,就是要准备尝试的和我一样的菜鸟们,可以真正的十分钟搞定.  当然高手可能用不上十分钟. 首先,就是 ...

  9. svn使用方法以及使用教程

    一.什么是svnSVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS. 二.svn的下载安装下载地址:https: ...

  10. IDEA(2018)连接MySQL数据库失败的解决方法(报错08001)

     解决方法: 将url改成: jdbc:mysql://localhost:3306/studentmanage?useSSL=true&serverTimezone=Hongkong& ...