题意

Language:Default
Lost Cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13448 Accepted: 8559

Description

N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.



Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.



Given this data, tell FJ the exact ordering of the cows.

Input

* Line 1: A single integer, N



* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.

Output

* Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.

Sample Input

5
1
2
1
0

Sample Output

2
4
5
3
1

Source

分析

从后往前考虑,如果第\(k\)头牛前面有\(A_k\)头比它矮,那么它的身高\(H_k\)是数值\(1\sim n\)中第\(A_k+1\)小没有在\(\{H_{k+1},H_{k+2},\dots,H_n\}\)中出现的数。

那么用01树状数组维护,每次倍增求第\(A_k+1\)小的就行了。

时间复杂度\(O(n \log n)\),USACO的数据是真的弱,n才8000。

代码

#include<iostream>
#include<cmath>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
    while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;

co int N=8e3+1;
int n,t,a[N],c[N],h[N],p[14];
void add(int x){
    while(x<=n) --c[x],x+=x&-x;
}
int main(){
//  freopen(".in","r",stdin),freopen(".out","w",stdout);
    p[0]=1;
    for(int i=1;i<14;++i) p[i]=p[i-1]<<1;
    t=log((float)read(n))/log(2.0);
    for(int i=1;i<=n;++i){
        ++c[i];
        if(i+(i&-i)<=n) c[i+(i&-i)]+=c[i];
    }
    a[1]=1;
    for(int i=2;i<=n;++i) a[i]=read<int>()+1;
    for(int i=n,ans,sum;i;--i){
        ans=sum=0;
        for(int j=t;j>=0;--j)
            if(ans+p[j]<=n&&sum+c[ans+p[j]]<a[i])
                sum+=c[ans+p[j]],ans+=p[j];
        add(h[i]=ans+1);
    }
    for(int i=1;i<=n;++i) printf("%d\n",h[i]);
    return 0;
}

POJ2182 Lost Cows的更多相关文章

  1. POJ2182 Lost Cows 题解

    POJ2182 Lost Cows 题解 描述 有\(N\)(\(2 <= N <= 8,000\))头母牛,每头母牛有自己的独一无二编号(\(1..N\)). 现在\(N\)头母牛站成一 ...

  2. [poj2182] Lost Cows (线段树)

    线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...

  3. [POJ2182]Lost Cows(树状数组,二分)

    题目链接:http://poj.org/problem?id=2182 题意:给定1~n个数和n个位置,已知ai表示第i个位置前有ai个数比当前位置的数小,求这个排列. 和刚才YY的题意蛮接近的,用树 ...

  4. hdu 2711&&poj2182 Lost Cows (线段树)

    从后往前查第一个为0的奶牛肯定应该排在第一个.每次从后往前找到第一个为0的数,这个数应该插在第j位.查找之后,修改节点的值为极大值,当整棵树的最小值不为0的时候查找结束. 至于这种查找修改的操作,再没 ...

  5. Poj2182 Lost Cows(玄学算法)

    题面 Poj 题解 不难发现最后一位就是\(pre[n]+1\),然后消除这个位置对其他位置的贡献,从左到右扫一遍,必定有至少一个位置可以得出,循环这个过程,\(O(n^2)\)出解. #includ ...

  6. POJ2182 Lost Cows 树状数组

    题意:有编号1~n乱序排列的奶牛,给出了每一个奶牛前小于自己编号的奶牛数目 维护一个树状数组,下标是编号,值为$0/1$标识是否存在,很显然最后一个牛的编号是知道的,我们在树状数组上二分出前缀和为小于 ...

  7. poj2182 Lost Cows[BIT二分]

    blog题解鸽了许久了..本来说好的切一题写一个题解的说 一个$1 \sim n$数列,已知每个数前面比他小的数个数,试确定该序列. 相当的一道水题.可以发现数列最后一个数是首先可以确定下来的.然后把 ...

  8. HDU 5497 Inversion

    Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description You have ...

  9. 线段树(Segment Tree)(转)

    原文链接:线段树(Segment Tree) 1.概述 线段树,也叫区间树,是一个完全二叉树,它在各个节点保存一条线段(即“子数组”),因而常用于解决数列维护问题,基本能保证每个操作的复杂度为O(lg ...

随机推荐

  1. SpringBoot中的数据库连接池

    内置的连接池 目前Spring Boot中默认支持的连接池有dbcp,dbcp2, tomcat, hikari三种连接池. 数据库连接可以使用DataSource池进行自动配置. 由于Tomcat数 ...

  2. 雷林鹏分享:Ruby 哈希(Hash)

    Ruby 哈希(Hash) 哈希(Hash)是类似 "employee" => "salary" 这样的键值对的集合.哈希的索引是通过任何对象类型的任意键 ...

  3. codeforces 1041d// Glider// Codeforces Round #509(Div. 2)

    题意:给出,n和飞行员高度h,n是区间数.在区间里飞行员高度不变,其它地方每秒高度-1,x坐标+1.问在高度变为0以前,x坐标最多加多少? 用数组gap记录本区间右端到下一个区间左端的距离.用sum记 ...

  4. Codeforces Round #364 (Div. 1) (差一个后缀自动机)

    B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...

  5. 『cs231n』无监督学习

    经典无监督学习 聚类 K均值 PCA主成分分析 等 深度学习下的无监督学习 自编码器 传统的基于特征学习的自编码器 变种的生成式自编码器 Gen网络(对抗式生成网络) 传统自编码器 原理 类似于一个自 ...

  6. mysql日期查询大全

    -- 查询昨日一整天的数据 DAY) ,'%Y-%m-%d 23:59:59') AS '昨日结束时间' -- 查询今日开始到当前时间的数据 DAY) ,'%Y-%m-%d %H:%i:%s') AS ...

  7. 隔离级别 && SNAPSHOT

    read uncommitted | 0 未提交读 将查询的隔离级别指定为 0. 可以读脏数据. 读脏数据:一事务对数据进行了增删改,但未提交,有可能回滚,另一事务却读取了未提交的数据.   例: 公 ...

  8. MySQL--Workbench表及表种数据导出

    MySQL--Workbench表及表种数据导出 1.-->点击Management,--->出现Navigator. 2.点击Date Export,选择导出的库. 3)选择库中的某个表 ...

  9. Idea检入boss项目

    Idea检入boss项目  

  10. Unity 3D连接MySQl数据库

    对数据库各种操作已经熟练,但是一遇到数据库问题还是头大,最近使用unity3d开发一款小型网络社区,遇到了各种问题分享一下以供大家参考: 以前使用的是SQL,第一次用MySQL,在网上随便下了一个,安 ...