关闭

丢失的牛?_Lost Cows_POJ2182_线段树

标签: pascalPOJ线段树
213人阅读 评论(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,它们并没有按照编号的顺序排好队列。现在只知道每一个牛前面有多少只牛的编号比它小。求所有牛的编号。

 

太水,懒得讲

源代码/pas:

 


#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
  readln(n);
  for i:=2 to n do read(a[i]);
  build(1,1,n);
end;
begin
  init;
  main;
end.


0
0
查看评论

Poj(2182)——Lost Cows(线段树)

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 neighbor...
  • ACMer_hades
  • ACMer_hades
  • 2015-05-30 10:59
  • 1010

Stage3d 中的DeviceLost(设备丢失)处理方案

/* @author Jave.Lin @date 2016-09-10 */IDeviceRes { function onRestore(newDevice:Context3D):void; }GameVertexBufferRes : IDeviceRes { rawData:...
  • linjf520
  • linjf520
  • 2016-09-10 16:32
  • 352

ORA-12547:TNS:lost contact

ORA-12547:TNS:lost contact sqlplus无法正常登陆数据库 解决思路如下: 1、查看操作系统内核参数是否无误 [oracle@normal adump]$ ulimit -a core file size         ...
  • ch7543658
  • ch7543658
  • 2014-08-28 17:50
  • 23893

POJ 2182 Lost Cows(牛排序,线段树)

POJ 2182 Lost Cows(牛排序,线段树)
  • u014737310
  • u014737310
  • 2014-08-22 16:21
  • 943

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

【问题描述】 IP地址从172.16.***.***转到了192.168.*.***。Hortonworks Sandbox 正常启动,进入Ambari浏览器页面(192.168.*.***:8080),会出现如下问题: 黄色问号表示:Heartbeat Lost 即所有的服务都心跳丢...
  • jxc928
  • jxc928
  • 2016-05-30 12:37
  • 3984

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

最近服务器很不稳定,于是重装了mysql 和php 服务,但是接着却遇到了很头疼的麻烦。 远程连接mysql是总是提示: 复制代码代码如下: Lost connection to MySQL server at ‘reading initial communication pack...
  • Heng_Ji
  • Heng_Ji
  • 2016-07-13 10:14
  • 527

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

Oracle dbca时报错:ORA-12547: TNS:lost contact
  • msdnchina
  • msdnchina
  • 2016-02-01 18:14
  • 5618

directx中丢失的设备(lost device)

   directx中丢失的设备(lost device) 丢失的设备一个Microsoft? Direct3D?可以处于操作状态或丢失状态。操作状态是设备的正常状态,设...
  • cglover
  • cglover
  • 2007-06-01 15:46
  • 3731

解决nodejs mysql Error: Connection lost The server closed the connection的两种方法

最近在学习node.js是发现在MySQL连接时出现问题,当过几个小时没有访问的MySQL的时候,MySQL自动断开连接,这个问题的原因是MySQL有一个wait_time当超过这个时间的时候连接会丢失,当你再去请求MySQL的时候会连接不上MySQL服务。先在整理一下解决这两个问题的方法: 一、...
  • wb_001
  • wb_001
  • 2018-01-08 10:54
  • 47

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

ERROR at line 1:  ORA-28511: lost RPC connection to heterogeneous remote agent using  SID=ORA-28511: lost RPC connection to heterogeneous ...
  • csdnhadoop
  • csdnhadoop
  • 2016-11-16 16:58
  • 776
    个人资料
    • 访问:115552次
    • 积分:6960
    • 等级:
    • 排名:第3961名
    • 原创:561篇
    • 转载:7篇
    • 译文:0篇
    • 评论:27条
    联系我
    QQ:315253566
    最新评论