zoukankan      html  css  js  c++  java
  • 1057. Stack (30) (浙大13年机试题)

    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:

    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
    

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

    开始被这题唬住了。。。。后来仔细想了想树状数组 + 二分 乱搞过了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;
    }
  • 相关阅读:
    phoenix 开发API系列 目录
    phoenix 开发API系列(三)phoenix api 结合数据库
    phoenix 开发API系列(二)phoenix 各类 api 实现方式
    phoenix 开发API系列(一)创建简单的http api
    golang 远程传输文件
    PAT-2019年冬季考试-甲级 7-3 Summit (25分) (邻接矩阵存储,直接暴力)
    PAT-2019年冬季考试-甲级 7-2 Block Reversing (25分) (链表转置)
    PAT-2019年冬季考试-甲级 7-1 Good in C (20分)
    PAT 甲级 树专题小结
    PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
  • 原文地址:https://www.cnblogs.com/vongang/p/2985196.html
Copyright © 2011-2022 走看看