1057 Stack
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的更多相关文章
- PAT 1057 Stack [难][树状数组]
1057 Stack (30)(30 分) Stack is one of the most fundamental data structures, which is based on the pr ...
- PAT甲级1057. Stack
PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
- pat 甲级 1057 Stack(30) (树状数组+二分)
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...
- PAT 1057. Stack (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057 用树状数组和二分搜索解决,对于这种对时间复杂度要求高的题目,用C的输入输出显然更好 #i ...
- 1057. Stack (30)
分析: 考察树状数组 + 二分, 注意以下几点: 1.题目除了正常的进栈和出栈操作外增加了获取中位数的操作, 获取中位数,我们有以下方法: (1):每次全部退栈,进行排序,太浪费时间,不可取. (2) ...
- PAT (Advanced Level) 1057. Stack (30)
树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
- 1057. Stack (30) - 树状数组
题目如下: Stack is one of the most fundamental data structures, which is based on the principle of Last ...
- PAT甲级1057 Stack【树状数组】【二分】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...
- 1057 Stack 树状数组
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
随机推荐
- ubantu16与windows下——redis的安装与使用
(1) 打开ubantu16,使用如下命令下载安装包 wget http://download.redis.io/releases/redis-2.8.3.tar.gz (2)解压缩命令: tar ...
- 推荐一款小众且好用的 Python 爬虫库 - RoboBrowser
1. 前言 大家好,我是安果! 今天推荐一款小众轻量级的爬虫库:RoboBrowser RoboBrowser,Your friendly neighborhood web scraper!由纯 Py ...
- BIMFACE二次开发【C#系列】
本系列文章主要介绍使用 C# .ASP.NET(MVC)技术对 BIMFACE 平台进行二次开发,以满足本公司针对建筑行业施工图审查系统的业务需求,例如图纸模型(PDF 文件.二维 CAD 模型.三维 ...
- python多线程参考文章
1. https://www.jianshu.com/p/c93e630d8089 2.https://www.runoob.com/python/python-multithreading.html ...
- python爬取三国演义的所有章节储存到本地文件中
#爬取三国演义的全部章节 2 3 import urllib 4 import urllib.request 5 import urllib.parse 6 from lxml import etre ...
- 如何在 ASP.Net Web Forms 中使用依赖注入
依赖注入技术就是将一个对象注入到一个需要它的对象中,同时它也是控制反转的一种实现,显而易见,这样可以实现对象之间的解耦并且更方便测试和维护,依赖注入的原则早已经指出了,应用程序的高层模块不依赖于低层模 ...
- linux软件deb打包及开机管理员自启动
环境:Ubuntu 18.04/16.04 Qt:5.12.6 一 deb打包 1.建立目录结构 2.目录内容 1) 子目录DC520: Get以上内容步骤: (1) 创建目录DC520(自己软 ...
- 攻防世界 reverse babymips
babymips XCTF 4th-QCTF-2018 mips,ida中想要反编译的化需要安装插件,这题并不复杂直接看mips汇编也没什么难度,这里我用了ghidra,直接可以查看反编译. 1 ...
- [题解] [NOI Online 2021 入门组 T3] 重力球
题目大意 在一个 \(n\times n\) 的矩形中,题目会给出 \(m\) 个障碍物.有两个小球,你可以选定四个方向(上下左右)的其中一个,小球会朝着这四个方向一直滚动,直到遇到障碍物或是矩形的边 ...
- 在 .NET Core 中使用 ViewConfig 调试配置
介绍 .NET Core 中的配置包含了多个配置提供程序,包括了 appsettings.json,环境变量,命令行参数等,还有一些扩展的自定义提供程序,比如说 ApolloConfig,AgileC ...