• POJ2182 Lost Cows 树状数组


    题意:有编号1~n乱序排列的奶牛,给出了每一个奶牛前小于自己编号的奶牛数目

    维护一个树状数组,下标是编号,值为$0/1$标识是否存在,很显然最后一个牛的编号是知道的,我们在树状数组上二分出前缀和为小于这个牛编号的奶牛数目的位置,这个位置代表的编号就是这头奶牛的编号。

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #include<cctype>
    #include<cstdlib>
    #include<vector>
    #include<queue>
    #include<map>
    #include<set>
    #define ull unsigned long long
    #define ll long long
    #define R register int
    #define pause (for(R i=1;i<=10000000000;++i))
    #define OUT freopen("out.out","w",stdout);
    using namespace std;
    namespace Fread {
        static char B[1<<15],*S=B,*D=B;
        #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
        inline int g() {
            R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
            do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
        } inline bool isempty(const char& ch) {return ch<=36||ch>=127;}
        inline void gs(char* s) {register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar()));}
    }using Fread::g; using Fread::gs; 
    const int N=8192;
    int n,c[N+1],a[N+1],pos,ans[N+1];
    inline int lbt(int x) {return x&-x;}
    inline void add(int pos) {for(;pos<=N;pos+=lbt(pos)) --c[pos];}
    inline int query(int vl) { R cnt=0,p=0;
        for(R k=12;~k;--k) {if(cnt+c[(1<<k)+p]<=vl) p+=1<<k,cnt+=c[p];} return p;
    }
    signed main() {
    #ifdef JACK
        freopen("NOIPAK++.in","r",stdin);
    #endif    
        n=g(); for(R i=2;i<=n;++i) a[i]=g(); 
        for(R i=1;i<N;++i) c[i]=1;
        for(R i=1;i<N;++i) c[i+lbt(i)]+=c[i]; 
        for(R i=n;i;--i) ans[i]=pos=query(a[i])+1,add(pos);
        for(R i=1;i<=n;++i) printf("%d
    ",ans[i]);
    }

    2019.06.27

  • 相关阅读:
    【181】IDL 代码从 Windows 转移到 Linux
    异步加载图片到GridView上,防止OOM
    10881
    AngularJS的开发工具---yeoman 简易安装
    AIX操作oracle
    hdu2817 A sequence of numbers
    FZU 2104 (13.11.28)
    HDU 1231 (13.12.2)
    Java 23种设计模式详尽分析与实例解析之一--创建型模式
    JSP视频
  • 原文地址:https://www.cnblogs.com/Jackpei/p/11048577.html
Copyright © 2020-2023  润新知