POJ 2182 Lost Cows 权值线段树

题目链接: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 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

解析

方法一:权值线段树

直接分析似乎很乱,但是我们可以从一般到特殊。最后一个点前面的点的总数是不是就是最后一个点的编号 − 1 -1 1?那么我们可以先确定最后一个点的编号。然后向前,这时候出现问题,倒数第二个节点不能直接和最后一个一样算,因为可能最后一个节点编号比它小,这样它就需要再在原基础上加一。但是我们仔细分析一下,可以随选随删,然后用权值线段树维护区间第 k k k大,这样,每次就可以直接查出编号并进行更新了。
代码:

#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn = 8e3 + 10;
int tree[maxn*8], p[maxn], res[maxn];

void build(int l, int r, int dex)
{
	if(l == r) tree[dex] = 1;
	else{
		int mid = (l+r)>>1;
		build(l, mid, dex*2), build(mid+1, r, dex*2+1);
		tree[dex] = tree[dex*2] + tree[dex*2+1];
	}
}
int query(int l, int r, int dex, int k)
{
	if(l == r) return l;
	int mid = (l+r)>>1;
	if(k <= tree[dex*2]) return query(l, mid, dex*2, k);
	else return query(mid+1, r, dex*2+1, k - tree[dex*2]);
 } 
void update(int l, int r, int dex, int x)
{
	if(l <= x && r >= x){
		tree[dex]--;
		if(l != r){
			int mid = (l+r)>>1;
			update(l, mid, dex*2, x), update(mid+1, r, dex*2+1, x);
		}
	}
 } 

int main()
{
	int n;
	while(scanf("%d", &n) != EOF){
		for(int i = 2; i <= n; i++) scanf("%d", &p[i]);
		p[0] = 0;
		build(1, n, 1);
		for(int i = n; i; i--){
			res[i] = query(1, n, 1, p[i]+1);
			update(1, n, 1, res[i]);
		}
		for(int i = 1; i <= n; i++) printf("%d\n", res[i]);		
	}
}

方法二:并查集

对,你没听错,这题还可以用并查集做。(其实是我半年前年轻的时候想的一个菜鸡的方法)
简单来说就是寻找父子关系,寻找编号在我前一个和后一个的元素,由于这种方法时间性能比方法一多一倍左右,这里就不过多介绍了,当做对当年菜鸡的自己(虽然现在也是)的怀念吧。
代码:

#include<cstdio>
#include<iostream>
using namespace std;
#define maxn 8000+5
int brnd[maxn], son[maxn], res[maxn];
int main()
{
	int n,cnt,lo,fa;
	scanf("%d",&n);
	son[1]=1,fa=1;
	for(int i=2;i<=n;i++) scanf("%d",&brnd[i]);
	for(int i=2;i<=n;i++){
		if(brnd[i]==0){
			son[i]=fa,fa=i;
			continue;
		}
		else if(brnd[i]==i-1){
			cnt=1,lo=fa;
			while(cnt<brnd[i]) lo=son[lo],cnt++;
			son[lo]=i;
		}
		cnt=1,lo=fa;
		while(cnt<brnd[i]) lo=son[lo],cnt++;
		son[i]=son[lo],son[lo]=i;
	}
	lo=fa;
	for(int i=1;i<=n;i++){
		res[lo]=i;
		lo=son[lo];
	}
	for(int i=1;i<=n;i++) printf("%d\n",res[i]);
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值