# 丢失的牛？_Lost Cows_POJ2182_线段树

143人阅读 评论(0)

Description

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

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

Output

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

Sample Input

5

1

2

1

0

Sample Output

2

4

5

3

1

Source

USACO 2003 U S Open Orange

n头牛，编号为1~n，它们并没有按照编号的顺序排好队列。现在只知道每一个牛前面有多少只牛的编号比它小。求所有牛的编号。

#include <stdio.h>
#include <stack>
using namespace std;
struct tree
{
int l,r,c;
}t[24001];
stack<int>ans;
int s[8001];
void build(int f,int x,int y)
{
t[f]=(tree){x,y,y-x+1};
int mid=(x+y)>>1;
if (x==y)
return;
build(f+f,x,mid);
build(f+f+1,mid+1,y);
}
int count(int f,int x)
{
--t[f].c;
if (t[f].l==t[f].r)
return t[f].r;
if (t[f+f].c<x)
return count(f+f+1,x-t[f+f].c);
else
return count(f+f,x);
}
int main()
{
int n;
scanf("%d",&n);
for (int i=2;i<=n;i++)
scanf("%d",&s[i]);
build(1,1,n);
for (int i=n;i>=1;i--)
ans.push(count(1,s[i]+1));
while (!ans.empty())
{
printf("%d\n",ans.top());
ans.pop();
}
return 0;
}


type
tree=record
l,r,c:longint;
end;
var
t:array[0..24000]of tree;
a:array[0..8001]of longint;
n:longint;
procedure build(f,x,y:longint);
var
mid:longint;
begin
t[f].l:=x;
t[f].r:=y;
t[f].c:=y-x+1;
mid:=(x+y)div 2;
if x=y then exit;
build(f*2,x,mid);
build(f*2+1,mid+1,y);
end;
function count(f,x:longint):longint;
begin
dec(t[f].c);
if t[f].l=t[f].r then
exit(t[f].l)
else
if t[f*2].c<x then
exit(count(f*2+1,x-t[f*2].c))
else
exit(count(f*2,x));
end;
procedure main;
var
i:longint;
s:array[0..8001]of longint;
begin
for i:=n downto 1 do
s[i]:=count(1,a[i]+1);
for i:=1 to n do
writeln(s[i]);
end;
procedure init;
var
i:longint;
begin
for i:=2 to n do read(a[i]);
build(1,1,n);
end;
begin
init;
main;
end.

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：61417次
• 积分：4560
• 等级：
• 排名：第6570名
• 原创：393篇
• 转载：7篇
• 译文：0篇
• 评论：25条
OJs
评论排行
最新评论