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

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

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

 

转载于:https://www.cnblogs.com/vongang/archive/2013/03/27/2985196.html

你可能感兴趣的文章
【Linux】ping命令详解
查看>>
Oracle中包的创建
查看>>
关于PHP会话:session和cookie
查看>>
jQuery on(),live(),trigger()
查看>>
treegrid.bootstrap使用说明
查看>>
[Docker]Docker拉取,上传镜像到Harbor仓库
查看>>
导航,头部,CSS基础
查看>>
[USACO 2017 Feb Gold] Tutorial
查看>>
gzip
查看>>
转负二进制(个人模版)
查看>>
LintCode-Backpack
查看>>
查询数据库锁
查看>>
我对于脚本程序的理解——百度轻应用有感
查看>>
面试时被问到的问题
查看>>
注解小结
查看>>
list control控件的一些操作
查看>>
一月流水账
查看>>
判断字符串在字符串中
查看>>
HashPump用法
查看>>
cuda基础
查看>>