Lost Cows

Openjudge 线段树练习题 Lost Cows


题目描述

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.

输入

* 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.

输出

* 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.

样例输入

5
1
2
1
0
样例输出

2
4
5
3
1


分析

根据输入数据能够轻松确定最后一头牛的编号是num[N-1]+1。当最后一头牛的编号确定后,倒数第二头牛的编号是剩下的N-1头牛中第num[N-2]+1小的。依次类推,可以确定每一头牛的编号。用一个len来记录每个区间未被牛占用的点的个数(在树叶处len为1,若某头牛的编号是该树叶所代表的数字,则会将len改为0,包括该点的区间的len都减1),若在某一结点处,若左子树的len>=num(num表示该牛编号是剩下的牛中第num小的),则该牛的编号在左子树表示的区间里(第num个未被占用的点),否则在右子树表示的区间里(第num-left.len个未被使用的点)。


AC代码

#include <iostream>
using namespace std;
struct node {
	int left, right, len;    //区间的左、右端点、未使用的点的长度 
}tree[32000];

void buildtree(int i, int l, int r)     //建树 
{
	tree[i].left = l;
	tree[i].right = r;
	tree[i].len = r-l+1;
	if (l < r)
	{
		int mid = (l+r)>>1, newi = i<<1;
		buildtree(newi,l,mid);        //左子树 
		buildtree(newi+1,mid+1,r);      //右子树 
	}
}

int querytree(int i, int num)      //访问树计算编号 
{
	tree[i].len--;     //未使用的数的长度减1 
	if (tree[i].left == tree[i].right)       //树叶(第num个数的编号) 
	return tree[i].left;
	int newi = i<<1;
	if (tree[newi].len >= num)     //第num个数在左子树 
	return querytree(newi,num);
	else
	return querytree(newi+1,num-tree[newi].len);     //第num个数在右子树(右子树的第num-tree[newi].len个数) 
}

int main()
{
	int N, i, num[8000], brand[8000];     //num为统计的数,brand为结果(每头牛的编号) 
	scanf("%d",&N);
	buildtree(1,1,N);      //建树 
	num[0] = 0;
	for (i = 1; i < N; ++i)     //N-1个统计数 
	scanf("%d",num+i);
	for (i = N-1; i >= 0; --i)    //从后到前计算编号 
	brand[i] = querytree(1,num[i]+1);
	for (i = 0; i < N; ++i)    //输出编号 
	printf("%d\n",brand[i]); 
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值