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;
    }
    
    
    
    
    
    
  • 相关阅读:
    C++学习笔记之 C++对C的增强和拓展
    C++学习笔记之 命名空间
    Python学习笔记之 MySQL
    C++学习笔记之 命名空间
    C++学习笔记之 开始
    C++学习笔记之 环境搭建
    C++学习笔记之 初识与概述
    数据结构与算法笔记(一) 数据结构与算法绪论
    C语言项目案例之 贪吃蛇
    C语言学习笔记之 文件操作
  • 原文地址:https://www.cnblogs.com/linruier/p/10098628.html
Copyright © 2011-2022 走看看