博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1057. Stack (30) (浙大13年机试题)
阅读量:6983 次
发布时间:2019-06-27

本文共 2954 字,大约阅读时间需要 9 分钟。

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 key

Pop
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 4PeekMedianPopPopPopPop

Sample Output:

InvalidInvalid322124453Invalid

 ----------------

开始被这题唬住了。。。。后来仔细想了想树状数组 + 二分 乱搞过了1Y。。

View Code
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define CL(arr, val) memset(arr, val, sizeof(arr))#define REP(i, n) for((i) = 0; (i) < (n); ++(i))#define FOR(i, l, h) for((i) = (l); (i) <= (h); ++(i))#define FORD(i, h, l) for((i) = (h); (i) >= (l); --(i))#define L(x) (x) << 1#define R(x) (x) << 1 | 1#define MID(l, r) (l + r) >> 1#define Min(x, y) (x) < (y) ? (x) : (y)#define Max(x, y) (x) < (y) ? (y) : (x)#define E(x) (1 << (x))#define iabs(x) (x) < 0 ? -(x) : (x)#define OUT(x) printf("%I64d\n", x)#define Read() freopen("data.in", "r", stdin)#define Write() freopen("data.out", "w", stdout);typedef long long LL;const double eps = 1e-6;const double PI = acos(-1.0);const int inf = ~0u>>2;using namespace std;const int N = 100010;int c[N];int lowbit(int i) { return i&(-i);}void add(int pos, int val) { while(pos < N) { c[pos] += val; pos += lowbit(pos); }}int sum(int pos) { int res = 0; while(pos > 0) { res += c[pos]; pos -= lowbit(pos); } return res;}char ss[20];int Stack[N], top;int vis[N];int find(int val) { int l = 0, r = N, mid, t; while(r - l > 1) { mid = (l + r) >> 1; t = sum(mid); if(t < val) l = mid; else r = mid; } return l + 1;}int main() { //Read(); CL(c, 0); CL(vis, false); int T, x, top = 0; scanf("%d", &T); while(T--) { scanf("%s", ss); if(strcmp(ss, "Push") == 0) { scanf("%d", &x); add(x, 1); vis[x] ++; Stack[++top] = x; } else { if(top == 0) { puts("Invalid"); continue; } if(ss[1] == 'o') { x = Stack[top--]; add(x, -1); vis[x]--; printf("%d\n", x); } else { printf("%d\n", find((top + 1)/2)); } } } return 0;}

 

转载地址:http://istpl.baihongyu.com/

你可能感兴趣的文章
磁盘格式化
查看>>
Fedora 11 安装指南-12
查看>>
eclipse的安卓开发插件『ADT』在线安装不成功的解决方案
查看>>
刷屏的海底捞超级APP究竟是怎样与阿里云合作的
查看>>
k8s学习笔记之三:k8s快速入门
查看>>
SpringBoot慕课学习-SpringBoot开发常用技术整合
查看>>
C10K问题
查看>>
慕课网3-13编程练习:采用flex弹性布局制作页面主导航
查看>>
线程中死锁的demo
查看>>
canvas-7globleCompositeOperation.html
查看>>
英语发音规则---H字母
查看>>
UESTC 2014 Summer Training #11 Div.2
查看>>
1035. 插入与归并(25)
查看>>
Android组件化和插件化开发
查看>>
【java】 虹软ArcFace 2.0 人脸信息识别(年龄、性别)
查看>>
Java集合--Map总结
查看>>
【转】Netty系列之Netty 服务端创建
查看>>
alpha冲刺9
查看>>
spring学习之spring 插件 for eclipse
查看>>
dispaly、position、float之间的关系与相互作用
查看>>