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;
    }
    
    
    
    
    
    
  • 相关阅读:
    HDU 4358 莫队算法+dfs序+离散化
    HDU 5692 线段树+dfs序
    Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分
    LVS负载均衡的三种模式和八种算法总结
    hdfs 常用命令
    Linux 系统监控
    CentOS 7 时区设置
    kubernetes 留言版DEMO
    CentOS7 PostgreSQL 主从配置( 三)
    Postgres数据库在Linux中优化
  • 原文地址:https://www.cnblogs.com/linruier/p/10098628.html
Copyright © 2011-2022 走看看