主题链接:http://poj.org/problem?

id=2182

Lost Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9152   Accepted: 5879

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

[Submit]   [Go Back]   [Status]  
[Discuss]

Home Page   Go
Back
  To top

线段树的一道经典题目。这个题目能够转化成从后向前依次查询,比方当前奶牛的前面有x个号码比它小的奶牛,那么它就应该在剩余的数的序列中排第x+1。当某个号码确定时,就在线段树中去除这个号码。我们遍历一下线段树若左子树区间内未删除元素个数满足当前要找的数成为第a+1个。能则递归左子树。否则递归右子树,直至到叶子节点。那么叶子节点的值就是其初始编号。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<vector>
#include<bitset>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<cstdlib>
using namespace std;
#define CLR(A) memset(A,0,sizeof(A))
const int MAX=8010;
struct Node{
int l,r,len,v;
}tree[MAX*4];
int ans[MAX];
int f[MAX];
inline void pushup(int id){
tree[id].len=tree[id<<1].len+tree[id<<1|1].len;
}
void build(int id,int l,int r){
tree[id].l=l;tree[id].r=r;
if(l==r){
tree[id].v=l;
tree[id].len=1;
return;
}
int m=(l+r)>>1;
build(id<<1,l,m);
build(id<<1|1,m+1,r);
pushup(id);
}
void query(int id,int cur,int pos){
if(tree[id].l==tree[id].r){
tree[id].len=0;
ans[pos]=tree[id].v;
return;
}
if(tree[id<<1].len>=cur) query(id<<1,cur,pos);
else query(id<<1|1,cur-tree[id<<1].len,pos);
pushup(id);
}
int main(){
int n;
while(~scanf("%d",&n)&&n){
for(int i=2;i<=n;i++){
scanf("%d",&f[i]);
f[i]++;
}
build(1,1,n);
f[1]=1;
for(int i=n;i>=1;i--){
query(1,f[i],i);
}
for(int i=1;i<=n;i++){
printf("%d\n",ans[i]);
}
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

poj 2182 Lost Cows(段树精英赛的冠军)的更多相关文章

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

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

  2. POJ 2182 Lost Cows (线段树)

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

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

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

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

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

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

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

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

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

  7. POJ 3264-Balanced Lineup(段树:单点更新,间隔查询)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34522   Accepted: 16224 ...

  8. POJ 2182 Lost Cows

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10996   Accepted: 7059 Description N (2 ...

  9. POJ 2777 Count Color(段树)

    职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...

随机推荐

  1. jQuery选择器实现隔行变色和使用javaScript实现隔行变色

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!--什么是选择器? jQuery选择器继承了 ...

  2. oschina jQuery 插件

    jQuery 插件 jQuery自动完成插件(25) jQuery分页插件(20) jQuery 文件上传(21) jQuery 地图插件(14) jQuery对话框(109) jQuery图片展示/ ...

  3. 完整导出IntelliJ IDEA的快捷键

    工欲善其事,必先利其器. 常常和代码打交道的人,熟练使用IDE快捷键那是必须的,由于快捷键能够把你从各种罗嗦事中解放出来.比方,假设没有快捷键,你就须要常常性的暂停快速执行的大脑,右手凭记忆摸到鼠标, ...

  4. 启动、停止、重启 MySQL 常见的操作方法:

    启动.停止.重启 MySQL 常见的操作方法: 简单罗列 一.启动方式 1.使用 service 启动:service mysqld start 2.使用 mysqld 脚本启动:/etc/inint ...

  5. Android的PackageManager的使用

    Android系统提供了很多服务管理的类,包括ActivityManager.PowerManager(电源管理).AudioManager(音频管理)以及PackageManager管理类.Pack ...

  6. 查看mysql当前表使用的存储引擎(转)

    说明:当我们创建表 “test”表时 CREATE TABLE test ( id INT(11) default NULL auto_increment, s char(60) default NU ...

  7. JDBC数据库编程常用接口(转)

    JDBC的全称是Java DataBase Connectivity,是一套面向对象的应用程序接口(API),制定了统一的访问各种关系数据库的标准接口,为各个数据库厂商提供了标准接口的实现.这东西能够 ...

  8. poj3176--Cow Bowling(dp:数塔问题)

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14028   Accepted: 9302 Desc ...

  9. WPF命中测试示例(二)——几何区域命中测试

    原文:WPF命中测试示例(二)--几何区域命中测试 接续上次的命中测试,这次来做几何区域测试示例. 示例 首先新建一个WPF项目,在主界面中拖入一个按钮控件,并修改代码中的以下高亮位置: 当前设计视图 ...

  10. PHP类中的七种语法说明

    类中的七种语法说明 -属性 -静态属性 -方法 -静态方法 -类常量 -构造函数 -析构函数 <?php class Student { // 类里的属性.方法和函数的訪问权限有 (函数和方法是 ...