zoukankan      html  css  js  c++  java
  • 1057 Stack (30 分)分桶法

    1057 Stack (30 分)

    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
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<queue>
    #include<string>
    #include<map>
    #include<set>
    #include<stack>
    #include<string.h>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    int cnt[100005];
    int bucket[100005];
    int Sqrt=113;//常数
    int main()
    {
        int n;
        scanf("%d",&n);
        stack<int>st;
       // st.reserve(n);
        for(int i=0; i<n; i++)
        {
            char ch[30];
            scanf("%s",ch);
            if(ch[1]=='o')
            {
                if(!st.empty())
                {
                    printf("%d
    ",st.top());
                    cnt[st.top()]--;
                    bucket[st.top()/Sqrt]--;
                    st.pop();
                }
                else
                    printf("Invalid
    ");
            }
            else if(ch[1]=='e')
            {
                if(!st.empty())
                {
                    int length=st.size();
                    int m=length%2==0?length/2:(length+1)/2;
                    int id=0,sum=0;
                    while(sum+bucket[id]<m) sum+=bucket[id++];
                    id=id*Sqrt;
                    while(sum+cnt[id]<m) sum+=cnt[id++];
                    printf("%d
    ",id);
                }
                else
                {
                    printf("Invalid
    ");
                }
            }
            else
            {
                int b;
                scanf("%d",&b);
                st.push(b);
                cnt[b]++;
                bucket[b/Sqrt]++;
                //minNum=minNum>b?b:minNum;
            }
        }
        return 0;
    }
     
  • 相关阅读:
    算法描述》关于LIS的nlogn方法
    简单tarjan》一道裸题(BZOJ1051)(easy)
    值得一做》关于数学与递推 BZOJ1002 (BZOJ第一页计划)(normal+)
    值得一做》关于一道DP+SPFA的题 BZOJ1003 (BZOJ第一页计划) (normal-)
    图论算法》关于最大流转最短路两三事
    刷题向》图论》BZOJ1001 平面图最大流、平面图最小割、单源最短路(easy+)
    c++中结构体的使用
    c++的标准流入流出和使用例子
    小知识 ——引用
    小知识——关于数组指针和指针数组
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10327136.html
Copyright © 2011-2022 走看看