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;
    }
    
    
    
    
    
    
  • 相关阅读:
    SVG Stroke属性
    C# 线程同步之排它锁/Monitor监视器类
    在Mac OS X Yosemite 10.10.3 中搭建第一个 ASP.NET 5 Web 项目
    jquery 之 Deferred 使用与实现
    jQuery 之 Callback 实现
    在解决方案中所使用 NuGet 管理软件包依赖
    下载和使用 Open XML PowerTools
    下载和编译 Open XML SDK
    Open XML SDK 在线编程黑客松
    VS2013 解决方案文件结构分析
  • 原文地址:https://www.cnblogs.com/linruier/p/10098628.html
Copyright © 2011-2022 走看看