# 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.

• 本文已收录于以下专栏：

举报原因： 您举报文章：poj 2182 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)