poj 2182

Description

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

Regrettably, FJ does not have a way to sort them. Furthermore, he's not verygood at observing problems. Instead of writing down each cow's brand, hedetermined a rather silly statistic: For each cow in line, he knows the numberof cows that precede that cow in line that do, in fact, have smaller brandsthan 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 givencow in line and have brands smaller than that cow. Of course, no cows precedethe first cow in line, so she is not listed. Line 2 of the input describes thenumber of preceding cows whose brands are smaller than the cow in slot #2; line3 describes the number of preceding cows whose brands are smaller than the cowin slot #3; and so on.

Output

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

Sample Input

5

1

2

1

0

Sample Output

2

4

5

3

1

type

pnode=^tnode;

tnode=record

lc,rc:pnode;

c:longint;

end;

const

maxn=800000;

var

t:pnode;

i,j,k,x,y,n:longint;

a,f:array [1..maxn] of longint;

procedure make(var p:pnode);

begin

ifp=nil then

begin

new(p);

p^.c:=0;

p^.lc:=nil;

p^.rc:=nil;

end;

end;

procedure insert(var t:pnode;l,r,cr:longint);

varmid:longint;

begin

witht^ do

begin

c:=cr;

mid:=(l+r) div 2;

ifl=r then exit;

make(lc);

make(rc);

insert(lc,l,mid,mid);

insert(rc,mid+1,r,cr);

end;

end;

function find(vart:pnode;l,r,cr:longint):longint;

var

mid:longint;

begin

witht^ do

begin

dec(c); ｛空位减一｝

if(lc=nil) and (rc=nil) then exit(l);

mid:=(l+r) div 2;

iflc^.c>=cr then find:=find(lc,l,mid,cr)       ｛判断左儿子是否可以放｝

else find:=find(rc,mid+1,r,cr+mid-lc^.c);｛cr+mid-lc^.c表示右儿子的空位｝｛但是要减去左儿子的空位｝

end;

end;

procedure init;

begin

fillchar(a,sizeof(a),0);

fillchar(f,sizeof(f),0);

t:=nil;

make(t);

insert(t,1,n,n);

for i:=1 to n-1 do

for i:=n-1 downto 1 do

f[i]:=find(t,1,n,a[i]+1);

writeln(find(t,1,n,1));

for i:=1 to n-1 do

writeln(f[i]);

end;

begin

init;

end.

poj2182

2015-09-01 23:30:57

poj 2182 Lost Cows 线段树

2016-05-28 19:49:15

poj 2985 树状数组+并查集（第k大组数）

2015-08-19 19:58:00

POJ 2985 The k-th Largest Group 第k大数 Treap / 树状数组 + 并查集

2017-09-03 23:13:12

poj 3662 二分+最短路，求第k大最小权值

2015-05-06 12:30:41

poj2104(区间第k大+离散化)

2015-07-29 18:35:52

POJ2985 The k-th Largest Group(平衡树查询第K大)

2015-11-22 16:25:34

hdu 2852 求第k大数值

2015-08-19 12:03:57

poj 2104 K-th Number 区间第K大 二分 离散化 + (莫队 树状数组/平方分解/线段树)

2016-02-29 21:55:23

POJ 题目2985 The k-th Largest Group（线段树单点更新求第k大值，并查集）

2015-08-03 10:28:28

poj 2182