1057. Stack (30)

来源:互联网 发布:网络缓存级别最高延迟 编辑:程序博客网 时间:2024/06/15 02:43

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
这道题目一开始怎么做都超时,网上一看才知道有树状数组这种东西,也是理解了一下,
树状数组:
add(index, x)以O(log(n))复杂度插入数据 
sum(x) 以O(log(n))计算所有小于等于x的和
至于这里用到的计算第k个最小值则是以hash方式将数字个数存入树状数组,所以这里得到的sum(x)则是数组小于等于x的个数,要找到第k个最小值即找到sum(x)从(<k-1)变化到(>=k)的那个值,用二分法寻找

#include <iostream>#include <string>#include <cstring>#include <stack>#include <cstdio>#define MAX 100005using namespace std;class BIT{public:    BIT(int s):Size(s)    {        tree = new int[Size]();    }    ~BIT()    {        delete [] tree;    }    void add(int index, int x)    {        while(index <= Size)        {            tree[index] += x;            index += lowbit(index);        }    }    int sum(int index)    {        int tempsum = 0;        while(index)        {            tempsum += tree[index];            index -= lowbit(index);        }        return tempsum;    }    int Find(int pos)//找到一个点比他小的数的数量    {        int left  = 1;        int right = MAX;        int mid;        while(left < right)        {            mid = (left+right)/2;            if(sum(mid) >= pos)//大于等于所求目标则中界为右界,这里右界不能等于mid-1,因为mid-1可能是我们所求目标                right = mid;            else                left = mid+1;//否则左界必定大于mid,从mid+1开始,左界不能从mid开始,当因左右界差2且mid+1为目标时,下次循环右界就变成目标了        }        return left;    }private:    int Size;    int *tree;    int lowbit(int x){return x&(-x);}};int main(){    int n, k;    int temp;    char func[20];    BIT mybit(MAX);    stack<int> mystack;    freopen("in.txt", "r", stdin);    scanf("%d", &n);    getchar();    for(int i=0; i<n; i++)    {        scanf("%s", func);        if(func[1] == 'o')        {            if(mystack.empty())            {                printf("Invalid\n");            }            else{                temp = mystack.top();                printf("%d\n", temp);                mystack.pop();                mybit.add(temp, -1);            }        }        else if(func[1] == 'e')        {            if(mystack.empty())            {                printf("Invalid\n");            }            else            {                k = (mystack.size()+1)/2;                printf("%d\n", mybit.Find(k));            }        }        else if(func[1] == 'u')        {            scanf("%d", &temp);            mystack.push(temp);            mybit.add(temp, 1);        }    }    return 0;}

0 0