poj 2182
2016-06-01 17:18
183 查看
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.
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.
相关文章推荐
- 数组
- quick cocos2dx脚本加密
- hadoop相关随记
- ES6学习笔记(六)--set,map数据结构和for...of遍历
- [从头学数学] 第232节 定积分
- 从0开始学习 GitHub 系列之「Git 速成」
- hadoop相关随记
- docker 监控浅谈
- 团体程序设计天梯赛-练习集 L3-003 社交集群 并查集 解题报告
- CSS3的nth-child() 和nth-of-type()选择器,表格奇偶行变色
- LA 4327 Parade (DP单调队列)
- appledoc 使用方法
- ES6学习笔记(五)--函数与对象
- LightOJ 1045 Digits of Factorial
- Struts 2中的constant详解
- mottoes
- Bootstrap: 样式CSS:carousel轮换 图片的使用
- Android 多线程断点下载及自动更新
- 【NOI题库 动态规划】
- iOS开发之单例使用(宏)