丢失的牛？_Lost Cows_POJ2182_线段树

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.

poj2182 Lost Cows (线段树)

2015-08-16 14:03:53

POJ2182 Lost Cows 线段树单点更新

2015-10-13 15:06:24

poj2182 Lost Cows(线段树)

2017-04-27 16:55:33

不更改数据库默认隔离级别，如何避免丢失更新（lost update）

2012-07-23 10:10:14

MySQL远程连接丢失问题解决方法（Lost connection to MySQL server）

2016-07-13 10:14:57

ACM大牛总结的线段树专辑

2015-07-20 14:03:50

更换服务器IP地址后,Ambari各项服务心跳丢失(heartbeat lost)的解决方案

2016-05-30 12:37:51

ORA-28511: lost RPC connection to heterogeneous remote agent using

2016-11-16 16:58:51

Oracle dbca时报错：ORA-12547: TNS:lost contact

2016-02-01 18:14:36

Oracle安装时出现ORA-12547: TNS:lost contact错误解决办法

2014-12-16 09:35:03