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. java与freemarker遍历map

    一.java遍历MAP /** * 1.把key放到一个集合里,遍历key值同时根据key得到值 (推荐) */ Set set =map.keySet(); Iterator it=set.iter ...

  2. vue3与vue2的区别

    全局属性 vue2 对于一些第三方插件,vue2中通常使用prototype原型来挂载到vue对象中 import Vue from 'vue' Vue.prototype.$http=Axiox V ...

  3. MySql-Day-01

    MySql 能够理解数据库的概念 能够安装MySQL数据库 能够启动,关闭及登录MySQL 能够使用SQL语句操作数据库 能够使用SQL语句操作表结构 能够使用SQL语句进行数据的添加修改和删除的操作 ...

  4. 9.Vue之webpack打包基础---模块化思维

    主要内容: 1. 什么是模块化思维? 2.  ES6包的封装思想 一.什么是模块化思维呢? 现实工作中, 一个项目可能会有多个人同时开发. 然后, 将所有人开发的内容, 合并到一个文件中. 比如: 1 ...

  5. POJ-1502(基本dijikstra算法)

    MPI Maelstrom POJ-1502 这题是求最短路,但是因为一开始看错题目,导致我去使用prime算法求最小生成树 题意是指一台机器发出信息后,还可以向其他的机器发送信息,所以不能使用pri ...

  6. HDOJ-3038(带权并查集)

    How many answers wrong HDOJ-3038 一个很好的博客:https://www.cnblogs.com/liyinggang/p/5327055.html #include& ...

  7. golang-Zap和Go Logger日志库

    目录 在Go语言项目中使用Zap日志库 介绍 默认的Go Logger日志库 实现Go Logger 设置Logger 使用Logger Logger的运行 Go Logger的优势和劣势 优势 劣势 ...

  8. 小技巧!CSS 整块文本溢出省略特性探究

    今天的文章很有意思,讲一讲整块文本溢出省略打点的一些有意思的细节. 文本超长打点 我们都知道,到今天(2020/03/06),CSS 提供了两种方式便于我们进行文本超长的打点省略. 对于单行文本,使用 ...

  9. P4285 [SHOI2008]汉诺塔 题解 (乱搞)

    题目链接 P4285 [SHOI2008]汉诺塔 解题思路 提供一种打表新思路 先来证明一个其他题解都没有证明的结论:\(ans[i]\)是可由\(ans[i-1]\)线性递推的. (\(ans[i] ...

  10. HDFS的上传流程以及windows-idea操作文件上传的注意

    HDFS的上传流程 命令:hdfs dfs -put xxx.wmv /hdfs的文件夹 cd进入到要上传文件的当前目录,再输入hdfs命令上传,注意-put后tab可以自动补全, 最后加上你要上传到 ...