1057. Stack (30) (浙大13年機試題)

來源:互聯網
上載者:User

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 <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <functional>#include <numeric>#include <sstream>#include <stack>#include <map>#include <queue>#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;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.