Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10996   Accepted: 7059

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

另开一个数组按编号顺序记录奶牛的状态,像这样: 1 1 1 1 1

算前缀和就能知道一头牛前面的牛数量。

倒着处理数据,若最后一头牛看到前面有0头编号更小的牛,通过上面的数组就可以知道这头牛的编号是1。

记录最后一头牛的编号,然后在数组中删去这头牛,数组变成0 1 1 1 1

以此类推

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int a[mxn];
int c[mxn];
int num[mxn];
int n;
inline int lowbit(int x){
return x&-x;
}
inline int sum(int x){
int i=x,res=;
while(i){
res+=c[i];
i-=lowbit(i);
}
// printf("sum[%d]==%d\n",x,res);
return res;
}
inline int find(int x){
int l=,r=n;
int mid;
while(l<r){
mid=((l+r)>>)+;
if(sum(mid)<=x)l=mid;
else r=mid-;
}
return l;
}
int main(){
scanf("%d",&n);
int i,j;
for(i=;i<n;i++){
scanf("%d",&a[i+]);
}
for(i=;i<=n;i++){//初始化
int tmp=i;
while(tmp<=n){
c[tmp]++;
tmp+=lowbit(tmp);
}
}
for(i=n;i>=;i--){
int tmp=find(a[i]+);
// printf("test %d %d\n",a[i],tmp);
num[i]=tmp++;
while(tmp<=n){
c[tmp]--;
tmp+=lowbit(tmp);
}
}
for(i=;i<=n;i++)printf("%d\n",num[i]);
return ;
}

POJ 2182 Lost Cows的更多相关文章

  1. poj 2182 Lost Cows(段树精英赛的冠军)

    主题链接:http://poj.org/problem? id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  2. POJ 2182 Lost Cows 【树状数组+二分】

    题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  3. POJ 2182 Lost Cows(牛排序,线段树)

    Language: Default Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9207   Acce ...

  4. POJ 2182 Lost Cows (线段树)

    题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ...

  5. POJ 2182 Lost Cows (求序列第k大)

    题解 二分+树状数组 显然最和一个数的值就是rank 那么其它数有什么规律? 从后往前匹配rank,我们可以发现第i个数的rank为还没有匹配的rank第(a[i]+1)大的数 这可以用 树状数组+二 ...

  6. 线段树/树状数组 POJ 2182 Lost Cows

    题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号: ...

  7. POJ 2182 Lost Cows (树状数组 && 二分查找)

    题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...

  8. Lost Cows POJ 2182 思维+巧法

    Lost Cows POJ 2182 思维 题意 是说有n头牛,它们身高不一但是排成了一队,从左到右编号为1到n,现在告诉你从第二号开始前面的那些牛中身高小于它的个数,一共有n-1个数.然后求出它们按 ...

  9. POJ 2182/暴力/BIT/线段树

    POJ 2182 暴力 /* 题意: 一个带有权值[1,n]的序列,给出每个数的前面比该数小的数的个数,当然比一个数前面比第一个数小的个数是0,省略不写,求真正的序列.(拗口) 首先想到的是从前到后暴 ...

随机推荐

  1. [转] 国外程序员整理的 C++ 资源大全

    关于 C++ 框架.库和资源的一些汇总列表,由 fffaraz 发起和维护. 内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了S ...

  2. Python 栅栏凯撒

    def fence_Crypto(msg,priority="row"): ''' usage: fence_Crypto(msg[, priority])->msg to ...

  3. grunt的使用方法,环境配置和插件安装

    虽然现在grunt的用的越来越少了,但是插件数量还是相当多的,另外grunt和gulp的使用相当相似: grunt需要安装node和npm 验证node是否安装:node -v 验证npm是否安装:n ...

  4. nginx反向代理+缓存开启+url重写+负载均衡(带健康探测)的部署记录

    在日常运维工作中,运维人员会时常使用到nginx的反向代理,负载均衡以及缓存等功能来优化web服务性能. 废话不多说,下面对测试环境下的nginx反向代理+缓存开启+url重写+负载均衡(带健康探测) ...

  5. AMAP

    ViewController.m #import "ViewController.h" //地图显示需要的头文件 #import <MAMapKit/MAMapKit.h&g ...

  6. 【C#】【Thread】Monitor和Lock

    所谓锁,就是之锁定的区域只能单个线程进入进行操作,其他线程在锁的外围等待.Monitor锁通过Monitor.Enter(obj)和Monitor.Exit(obj)来锁定和解锁.Lock锁则直接Lo ...

  7. [py] 导入模块 reload(sys)

        #!/usr/bin/env python # coding: utf-8 import sys   reload(sys) #<------这个是什么意思 sys.setdefault ...

  8. [CareerCup] 13.9 Aligned Malloc and Free Function 写一对申请和释放内存函数

    13.9 Write an aligned malloc and free function that supports allocating memory such that the memory ...

  9. Linux 基础入门(新版)”实验报告一~十二

    实验报告 日期: 2015年9月15日 一.实验的目的与要求 熟练地使用 Linux,本实验介绍 Linux 基本操作,shell 环境下的常用命令. 二.主要内容 1.Linux 基础入门& ...

  10. Openwrt 无法上40MHZ带宽

    最近有一个纠结的事情是双天线路由没法上40MHZ带宽,怎么改都不成功,其中 在无线高级设置里面打开了40MHZ带宽: 后来在客户端连上之后,在无线的页面一直显示都是20M的带宽.于是我想是不是驱动或者 ...