关闭

poj 2182

86人阅读 评论(2) 收藏 举报
分类:

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

 

题目大意:给出n头牛,且告诉你每头牛前面有多少头比他小,求每头牛的编号。(第一头可以看为0)。

 

分析:我们可以用一棵线段树,记录一个c,用来表示该节点有多少个空位。如果一个点前有k个比它小的编号,那么他就是k+1个,也就是要插入到第k+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);

   readln(n);

   t:=nil;

   make(t);

   insert(t,1,n,n);

   for i:=1 to n-1 do

    readln(a[i]);

   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.

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:37064次
    • 积分:3496
    • 等级:
    • 排名:第9836名
    • 原创:317篇
    • 转载:0篇
    • 译文:0篇
    • 评论:139条