poj 2182

原创 2016年06月01日 17:18:32

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.

 

版权声明:2333

相关文章推荐

poj2182

题目大意:有n头牛,给出从第二头牛开始每头牛前面有多少头牛编号比自己小。 求牛的编号。 分析:每次最后一头牛的编号为当前可用编号中排在第a[i]+1的数。 -------->线段树 O(nlog...

poj2182之二分+树状数组

Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8712   Acce...

POJ2182 逆推数列

LOST COWS N (2

POJ 2182 Lost Cows(线段树)

//线段树 //题意:给定数列的顺序数,进而确定数列的排列 //思路:逆向思考,对给定的N个数,从第N个数入手,如果他的顺序数是k,则说明他在1~N中排第k+1位 //确定一个元素的位置K后,从...

POJ2182 Lost Cows 线段树单点更新

题目链接:http://poj.org/problem?id=2182 题目大意:有编号为1到n的n头牛按照某一编号排列,现在他们的顺序打乱了,FJ只记得每头牛前面有多少个牛的编号比它大,让...

poj2182

本题题意很简单,就是给你一个N-1的序列,表示从第二个数开始,这个数前面有多少个比自己小的数。求这个序列。这题一开始我以为是组合数学的逆序列,不过其实也差不多。后来想了一个算法不过错了,那就不说了。这...

POJ 2182 Lost Cows

树状数组
  • Rlt1296
  • Rlt1296
  • 2016年07月09日 20:25
  • 168

poj2182解题报告

题目大姨:n头牛排队,给定从2~n头牛每头牛前面比它号码小的牛的头数,确定这队牛的排队方式 Sample Input5 1 2 1 0 Sample Output2 4 5 3 1 ...

POj 2182 线段树

题意:题意:FJ有n头牛,编号为1~n,它们并没有按照编号的顺序排好队。现在,FJ只知道每一个牛前面有多少只牛的编号比它大。问你能不能判断出所有牛的编号。 思路:采用线段树。。假设一个编号高度为a[i...

poj 2182(单点修改)

题意:有n头牛,标号从1到n,现在牛排成一个队列,知道每个牛的前面比自己标号小的牛的数量,输出每个牛的标号。 题解:给出的序列的最后一个数字是可以推出的,然后把这个数字拿走,又能知道前面有几个数字,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 2182
举报原因:
原因补充:

(最多只允许输入30个字)