zoukankan      html  css  js  c++  java
  • pat 甲级 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 Nis odd.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive integer N (≤10​5​​). 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 10​5​​.

    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 Invalidinstead.

    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<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int st[100005];
    ll bit[1000005];
    ll n;
    void add(ll i,ll x)
    {
        while(i<=n)
        {
            bit[i]+=x;
            i+=i&-i;
        }
    }
    ll sum(ll i)
    {
        ll s=0;
        while(i>0)
        {
            s+=bit[i];
            i-=i&-i;
        }
        return s;
    }
    int pos=0;
    int main()
    {
        n=100000;
        //freopen("in.txt","r",stdin);
        int N;
        cin>>N;
        string op;
        while(N--)
        {
            cin>>op;
            if(op[1]=='o')
            {
                if(pos==0){cout<<"Invalid"<<endl;continue;}
                cout<<st[pos]<<endl;
                add(st[pos],-1);
                pos--;
            }
            else if(op[1]=='u')
            {
                int num;
                cin>>num;
                add(num,1);
                st[++pos]=num;
            }
            else if(op[1]=='e')
            {
                if(pos==0){cout<<"Invalid"<<endl;continue;}
                int x=(pos%2==0)?pos/2:(pos+1)/2;
                int lb=0;int ub=100001;
                while(ub-lb>1)
                {
                    int mid=(ub+lb)/2;
                    if(sum(mid)<x)lb=mid;
                    else ub=mid;
                }
                cout<<ub<<endl;
            }
            else {
                cout<<"Invalid"<<endl;continue;
            }
        }
        return 0;
    }
    
    
    
    
    
    
  • 相关阅读:
    python pycurl属性
    HTTP报文-->MVC
    国际化信息-->MVC
    计算机网络入门概念理解
    TCP/IP基础概念及通信过程举例
    一道面试题:说说进程和线程的区别
    【转载】我眼中的Oracle性能优化
    【转载】数据库范式那些事
    【转载】详解CreateProcess调用内核创建进程的过程
    数据结构和算法(一):hash散列容器
  • 原文地址:https://www.cnblogs.com/linruier/p/10098628.html
Copyright © 2011-2022 走看看