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 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

题意:

  模拟一个stack,并且新增一个返回中值的功能。

思路:

  如果每次查询中值都sort一次的话,只能通过两组测试点。看了别人的blog之后发现这道题有很多种做法,其中较好理解的一种就是用二维桶排序的方法来做这道题。大体的思路是这样的:首先划分出100个大桶,每一个大桶中存放1000个小桶。用数组下标来表示栈中的数字,数组的值来表示出现的次数。另外还要用一个栈来模拟push和pop,桶排序用来查找中值。先找出目标数字出现在那个大桶中,然后再在大桶中找出在那个小桶中。

Code:

 1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 int main() {
6 int n;
7 cin >> n;
8 getchar();
9 string str;
10 stack<int> stk;
11 int prt = -1;
12 int bucket_count[100] = {0};
13 int bucket[100][1000] = {0};
14 for (int i = 0; i < n; ++i) {
15 getline(cin, str);
16 if (str[1] == 'u') {
17 int num = stoi(str.substr(5));
18 bucket_count[num / 1000]++;
19 bucket[num / 1000][num % 1000]++;
20 stk.push(num);
21 } else if (str[1] == 'o') {
22 if (stk.empty())
23 cout << "Invalid" << endl;
24 else {
25 int num = stk.top();
26 cout << num << endl;
27 bucket_count[num / 1000]--;
28 bucket[num / 1000][num % 1000]--;
29 stk.pop();
30 }
31 } else {
32 if (stk.empty()) {
33 cout << "Invalid" << endl;
34 } else {
35 int target = (stk.size() + 1) / 2;
36 int count = 0;
37 for (int i = 0; i < 100; ++i) {
38 if (count + bucket_count[i] >= target) {
39 for (int j = 0; j < 1000; ++j) {
40 if (count + bucket[i][j] >= target) {
41 cout << i * 1000 + j << endl;
42 break;
43 }
44 count += bucket[i][j];
45 }
46 break;
47 }
48 count += bucket_count[i];
49 }
50 }
51 }
52 }
53
54 return 0;
55 }

1057 Stack的更多相关文章

  1. PAT 1057 Stack [难][树状数组]

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

  2. PAT甲级1057. Stack

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

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

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

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

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

  5. PAT 1057. Stack (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057 用树状数组和二分搜索解决,对于这种对时间复杂度要求高的题目,用C的输入输出显然更好 #i ...

  6. 1057. Stack (30)

    分析: 考察树状数组 + 二分, 注意以下几点: 1.题目除了正常的进栈和出栈操作外增加了获取中位数的操作, 获取中位数,我们有以下方法: (1):每次全部退栈,进行排序,太浪费时间,不可取. (2) ...

  7. PAT (Advanced Level) 1057. Stack (30)

    树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...

  8. 1057. Stack (30) - 树状数组

    题目如下: Stack is one of the most fundamental data structures, which is based on the principle of Last ...

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

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

  10. 1057 Stack 树状数组

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

随机推荐

  1. 浮动引发的高度塌陷问题及其解决方法(BFC相关概念及性质)

    浮动引发的高度塌陷问题 高度塌陷问题的产生 BFC(Block Formatting Context)的引入 元素开启BFC后的特点 开启BFC的元素不会被其他浮动元素所覆盖 开启BFC的元素不会发生 ...

  2. Flask:处理Web表单

    尽管 Flask 的请求对象提供的信息足以处理 Web 表单,但有些任务很单调,而且要重复操作.比如,生成表单的 HTML 代码和验证提交的表单数据.Flask-WTF 扩展可以把处理 Web 表单的 ...

  3. javascript中的闭包closure详解

    目录 简介 函数中的函数 Closure闭包 使用闭包实现private方法 闭包的Scope Chain 闭包常见的问题 闭包性能的问题 总结 简介 闭包closure是javascript中一个非 ...

  4. 面试常备,字符串三剑客 String、StringBuffer、StringBuilder

    尽人事,听天命.博主东南大学硕士在读,热爱健身和篮球,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收录于 「CS-Wiki」Gitee ...

  5. JVM笔记 -- JVM的发展以及基于栈的指令集架构

    2011年,JDK7发布,1.7u4中,开始启用新的垃圾回收器G1(但是不是默认). 2017年,发布JDK9,G1成为默认GC,代替CMS.(一般公司使用jdk8的时候,会通过参数,指定GC为G1) ...

  6. Git代码分支开发工作流程

    本文的工作流程,有一个共同点:都采用"功能驱动式开发"(Feature-driven development,简称FDD). 它指的是,需求是开发的起点,先有需求再有功能分支(fe ...

  7. python基础学习之文件的基础操作方法

    打开文件方法 open('xx') 注意,open后括号内加的是文件名,这里默认是当前文件的相对路径,如果不在当前文件层,需要绝对路径,默认打开方法是读取,即read,默认的解码器为当前系统的解码器w ...

  8. java 集合 的理解

    1.对象的存储:①数组(基本数据类型 & 引用数据类型) ②集合(引用数据类型) >数组存储数据的弊端:长度一旦初始化以后,就不可变:真正给数组元素赋值的个数没有现成的方法可用. 2.集 ...

  9. 热门跨平台方案对比:WEEX、React Native、Flutter和PWA

    本文主要对WEEX.React Native.Flutter和PWA几大热门跨平台方案进行简单的介绍和对比.内容选自<WEEX跨平台开发实战> (WEEX项目负责人力荐,从入门到实战,教你 ...

  10. Tornado 简明教程

    1.TornadoTornado:python编写的web服务器兼web应用框架1.1.Tornado的优势轻量级web框架异步非阻塞IO处理方式出色的抗负载能力优异的处理性能,不依赖多进程/多线程, ...