1. 程式人生 > >1057 Stack (分塊思想)

1057 Stack (分塊思想)

out esp col file nts return while com eve

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 (-th smallest element if N is even, or (-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 (≤). 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 1.

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

  

由於N<=10^5,暴力算法肯定超時,我一開始思考用multiset容器可以自動排序,只要找到中值即可

#include<cstdio>
#include<set>
#include<cstring>
#include<stack>
using namespace std;

stack<int> st;
multiset<int> se; 
int tot=0;

int main(){
    int n,m;
    char s[20];
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",s);
        if(strcmp(s,"Pop")==0){
            if(tot==0) printf("Invalid\n");
            else{
                int x=st.top();
                st.pop();
                se.erase(x);
                tot--;
                printf("%d\n",x);
            }
        }else if(strcmp(s,"Push")==0){
            scanf("%d",&m);
            st.push(m);
            se.insert(m);
            tot++;
        }else{
            if(tot==0) printf("Invalid\n");
            else{
                int k=tot;
                if(k%2==1) k=(k+1)/2;
                else k=k/2;
                multiset<int>::iterator it=se.begin();
                for(int i=1;i<=k-1;i++)
                    it++;
                int x=*it;
                printf("%d\n",x);
            }
        }
    }
    return 0;
}

技術分享圖片

set容器不能直接訪問中值,這是由於底層是平衡樹的原因,看來實際效果不好,學習了書上的分塊思想,復雜度可達O(N√N)

#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int maxn=100010;
const int sqrN=316;

stack<int> st;
int block[sqrN];
int hash[maxn];

void Push(int x){
    st.push(x);
    hash[x]++;
    int c=x/sqrN;
    block[c]++; 
}

void Pop(){
    int x=st.top();
    printf("%d\n",x);
    st.pop();
    block[x/sqrN]--;
    hash[x]--;
}

void peekMedian(int k){//第k小 
    int sum=0;
    int idx=0;
    while(sum+block[idx]<k) sum+=block[idx++];
    int num=idx*sqrN;
    while(sum+hash[num]<k) sum+=hash[num++];
    printf("%d\n",num);
} 

int main(){
    int n,m;
    char s[20];
    scanf("%d",&n);
    memset(block,0,sizeof(block));
    memset(hash,0,sizeof(hash));
    for(int i=1;i<=n;i++){
        scanf("%s",s);
        if(strcmp(s,"Pop")==0){
            if(st.size()==0) printf("Invalid\n");
            else Pop();
        }else if(strcmp(s,"Push")==0){
            scanf("%d",&m);
            Push(m);
        }else{
            if(st.size()==0) printf("Invalid\n");
            else{
                int k=st.size();
                if(k%2==1) k=(k+1)/2;
                else k=k/2;
                peekMedian(k);
            }
        }
    }
    return 0;
}

1057 Stack (分塊思想)