PAT(A) - 1057. Stack (30)
来源:互联网 发布:机械行业转行it 编辑:程序博客网 时间:2024/06/05 02:59
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 keyPop
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:17PopPeekMedianPush 3PeekMedianPush 2PeekMedianPush 1PeekMedianPopPopPush 5Push 4PeekMedianPopPopPopPopSample Output:
InvalidInvalid322124453Invalid
思路分析:直接暴力会超时,我以前参考别人的代码,利用树状数组做的。今天看了晴神的书,用的分块思想,好神奇呀~
#include <cstdio>#include <stack>#include <cmath>#include <string.h>#define MAX 100000 + 10using namespace std;int block[316];int table[MAX];int sqr = ( int )sqrt( MAX );stack<int> s;using namespace std;void Push( int num ) { s.push( num ); block[num / sqr]++; table[num]++;}void Pop() { if( s.empty() ) { printf( "Invalid\n" ); return; } int top = s.top(); s.pop(); table[top]--; block[top / sqr]--; printf( "%d\n", top );}void PeekMedian() { if( s.empty() ) { printf( "Invalid\n" ); return; } int k = s.size(); if( k % 2 == 1 ) k = ( k + 1 ) / 2; else k = k / 2; int sum = 0; int id = 0; while( sum + block[id] < k ) { sum = sum + block[id]; id++; } int num = id * sqr; while( sum + table[num] < k ) { sum += table[num]; num++; } printf( "%d\n", num );}int main() { //freopen( "123.txt", "r", stdin ); int n; scanf( "%d", &n ); char op[20]; int num; for( int i = 0; i < n; i++ ) { scanf( "%s", op ); if( !strcmp( op, "Push" ) ) { scanf( "%d", &num ); Push( num ); } else if( !strcmp( op, "Pop" ) ) { Pop(); } else if( !strcmp( op, "PeekMedian" ) ) { PeekMedian(); } } return 0;}
- PAT A 1057. Stack (30)
- PAT(A) - 1057. Stack (30)
- PAT(A) - 1057. Stack (30)
- PAT-A-1057. Stack (30)
- 1057. Stack (30)-PAT
- pat 1057. Stack (30)
- PAT 1057. Stack (30)
- 【PAT】1057. Stack (30)
- PAT 1057. Stack (30)
- PAT (Advanced) 1057. Stack (30)
- 【PAT甲级】1057. Stack (30)
- PAT甲级1057. Stack (30)
- PAT 甲级 1057. Stack (30)
- 浙大2013复试:PAT 1057. Stack (30)
- PAT甲级练习1057. Stack (30)-未完成
- PAT 1057. Stack (30) 中位数;树状数组
- 【PAT】【Advanced Level】1057. Stack (30)
- PAT 1057. Stack
- 博客利器Hexo的使用
- Android 根据View(布局) 生成图片(Bitmap)
- UINavigationBar 使用总结
- 关于本地文件请求json文件
- 利用aws一年免费EC2搭建自用VPN
- PAT(A) - 1057. Stack (30)
- day01HTML和CSS总结
- 如何胜任一个小型公司的技术总监
- 深度学习笔记(五)第五章 深度学习基础
- Octotree实用工具推荐GitHub/GitLab 代码结构浏览插件
- java平台的编码问题 getByte()所用编码
- Mouse Hook
- 打印分别由signed和unsigned限定的char , short, int ,与long类型变量的取值范围。
- ORACLE 11g 高可用Data Guard在线降级详细过程