[PAT考试]1057. Stack (30)

内容纲要

1057. Stack (30)

时间限制

100 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x) {return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}
struct BinTree
{
    static const int MAXN = 100010;
    std::vector<int> a;
    BinTree()
    {
        a = std::vector<int>(MAXN, 0);
    }
    int lowbit(int t)
    {
        return t & (-t);
    }
    void update(int t, int d)
    {
        while (t <= MAXN)
        {
            a[t] += d;
            t += lowbit(t);
        }
    }
    int getsum(int t)
    {
        int sum(0);
        while (t > 0)
        {
            sum += a[t];
            t -= lowbit(t);
        }
        return sum;
    }
    int find(int val, int l = 0, int h = MAXN - 1)
    {
        if (l == h)
            return l;
        int mid = (l + h) / 2;
        if (getsum(mid) < val)
            return find(val, mid + 1, h);
        else
            return find(val, l, mid);
    }
};
BinTree tree;
int main()
{
#ifdef DeBUGs
    freopen("/Users/sky/Documents/sublime project/in.txt", "r", stdin);
#endif
    int N;
    scanf("%d", &N);
    char str[20];
    int n(0);
    stack<int>stk;
    while (N--)
    {
        scanf("%s", str);
        switch (str[1])
        {
        case 'o':
            if (stk.empty())
            {
                printf("Invalid\n");
            }
            else
            {
                n = stk.top();
                printf("%d\n", n);
                stk.pop();
                tree.update(n, -1);
            }
            break;
        case 'u':
            scanf("%d", &n);
            stk.push(n);
            tree.update(n, 1);
            break;
        case 'e':
            if (stk.empty())
                printf("Invalid\n");
            else
            {
                printf("%d\n", tree.find((stk.size() + 1) / 2));
            }
            break;
        }
    }
    return 0;
}

发表回复