• 树状数组 | 1057


    用哈希,把push的数x作为下标给hashTable(实则不存在,直接用tree树状数组记录数据)+1,pop则是以最后一个数x作为下标-1 。

    树状数组和其原理不再赘述,需要注意的是最后的二分搜索(实则是lower_bound)中位数。

    #include <stdio.h>
    #include <memory.h>
    #include <math.h>
    #include <string>
    #include <cstring>
    #include <vector>
    #include <set>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <map>
    
    
    #define I scanf
    #define OL puts
    #define O printf
    #define F(a,b,c) for(a=b;a<c;a++)
    #define FF(a,b) for(a=0;a<b;a++)
    #define FG(a,b) for(a=b-1;a>=0;a--)
    #define LEN 100010
    #define MAX (1<<30)+1
    #define V vector<int>
    
    using namespace std;
    
    int tree[LEN];
    
    int lowbit(int x){
        return x&-x;
    }
    
    int getSum(int p){
        int sum=0;
        while(p>0){
            sum+=tree[p];
            p-=lowbit(p);
        }
        return sum;
    }
    
    void update(int p,int x){
        while(p<LEN){    //对于有确定边界的树状数组,应该是 p<=N ,但是这题不用考虑这些 
            tree[p] +=x;
            p+=lowbit(p);
        }
    }
    
    char buf[100];
    stack<int> s;
    
    void PeekMedian(){
        int l=1,r=LEN,mid,k=(s.size()+1)/2;
        while(l<r){
            mid=(l+r)/2;
            if(getSum(mid)<k){
                l=mid+1;
            }else{
                r=mid;
            }
        }
        O("%d
    ",l);
    }
    
    int main(){
    //    freopen("1057.txt","r",stdin);
        int N,t;
        I("%d",&N);
        while(N--){
            I("%s",buf);
            if(strcmp(buf,"Pop")==0){
                if(s.empty()) puts("Invalid");
                else{
                    t=s.top();
                    O("%d
    ",t);
                    s.pop();
                    update(t,-1);
                }
            }else if(strcmp(buf,"Push")==0){
                I("%d",&t);
                update(t,1);
                s.push(t);
            }else{
                if(s.empty()) puts("Invalid");
                else PeekMedian();
            }
        }
        return 0;
    }
  • 相关阅读:
    2015 HUAS Summer Contest#2~B
    2015 HUAS Summer Contest#2~A
    HUAS Summer Trainning #3~B
    HUAS Summer Trainning #3~A
    2015 HUAS Provincial Select Contest #1~D
    UVA 725
    货币体系
    N皇后摆放问题
    种子填充找连通块 floodfill
    二叉树的递归遍历,用先序和中序输出后序
  • 原文地址:https://www.cnblogs.com/TQCAI/p/8573673.html
Copyright © 2020-2023  润新知