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;
    }
  • 相关阅读:
    thrift ssl 证书整理
    snmp学习笔记
    你必须了解的Session的本质
    安装大文件提示错误
    说说大型高并发高负载网站的系统架构(更新)
    【错误】未能找到类型或命名空间名称XXXX和未能解析引用的程序
    js调用后台代码的几种方式
    HTTP 错误 401.1
    万能的编程语言不要习惯性地把工具当作你天性缺陷的遮羞布
    PostgreSQL与MySQL比较
  • 原文地址:https://www.cnblogs.com/vongang/p/2985196.html
Copyright © 2011-2022 走看看