POJ-2182 Lost Cows (二分 + 树状数组 或者平衡树)

Lost Cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12063 Accepted: 7760

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
#include <stdio.h>  
#include <string.h>  
#include <algorithm>
using namespace std;
#define maxn 8005
int a[maxn], c[maxn], ans[maxn];
inline int lowbit(int x){ return x & (-x);}
int getsum(int x){
	int ans = 0;
	while(x){
		ans += c[x];
		x -= lowbit(x);
	}
	return ans;
}
void add(int x, int n, int v){
	while(x <= n){
		c[x] += v;
		x += lowbit(x);
	}
}
int main(){
	int n, l, r, mid;
	scanf("%d", &n);
	memset(c, 0, sizeof(c));
	a[1] = 0;
	for(int i = 2; i <= n; ++i){
		scanf("%d", &a[i]);
	}
	for(int i = n; i >= 1; --i){
		l = 1; r = n;
		while(r > l){
			mid = l + r >> 1;
			if(mid - getsum(mid) > a[i]) r = mid;
			else l = mid + 1;
		}
		ans[i] = l;
		add(l, n, 1);
	}
	for(int i = 1; i <= n; ++i){
		printf("%d\n", ans[i]);
	}
}

/*
题意:给出序列上每个数前面有多少个数比它小,让你还原这个序列。

思路:做这题需要发现首先最后一位是可以唯一确定的,那么我们倒着来还原就行了。
对于每一位,我们需要找出在还未使用的数字中,排第a[i]位的数字是多少。暴力枚举哪些数字没用的话是n方了。
这题用平衡树来写清晰一点,但可以用二分树状数组来做。二分答案mid,如果前面没用的数字大于a[i]就向前枚举,
否则向后枚举。用过的数就在相应位置加1,这样就可以用树状数组求前缀和来统计没用过的数。

下面也给出平衡树splay写的AC代码。
*/

Splay代码:
#include <stdio.h>
#define maxn 10000
int pre[maxn], key[maxn], ch[maxn][2], sz[maxn], rt, tot, a[maxn], ans[maxn];
void newNode(int& r, int father){ 
	r = ++tot;
	pre[r] = father;
	sz[r] = 1;
	ch[r][0] = ch[r][1] = 0;
}
void rotate(int x, int kind){
	int y = pre[x];
	ch[y][!kind] = ch[x][kind];
	pre[ch[x][kind]] = y;
	if(pre[y]){  //do it when y is not root
		ch[pre[y]][ch[pre[y]][1] == y] = x;
	}
	pre[x] = pre[y];
	ch[x][kind] = y;
	pre[y] = x;

	sz[x] = sz[y];
	sz[y] = sz[ch[y][0]] + sz[ch[y][1]] + 1;
}
void splay(int r, int goal){ 
	while(pre[r] != goal){
		if(pre[pre[r]] == goal){
			rotate(r, ch[pre[r]][0] == r);
		}
		else{
			int y = pre[r];
			int kind = ch[pre[y]][0] == y;
			if(ch[y][kind] == r){
				rotate(r, !kind);
				rotate(r, kind);
			}
			else{
				rotate(y, kind);
				rotate(r, kind);
			}
		}
	}
	if(goal == 0) rt = r;
}
void insert(int x){
	int r = rt;
	while(1){
		if(x < r){
			if(ch[r][0]) r = ch[r][0];
			else break;
		}
		else{
			if(ch[r][1]) r = ch[r][1];
			else break;
		}
	}
	newNode(ch[r][x > r], r);
	splay(ch[r][x > r], 0);
}
void find(int x){
	int r = rt;
	while(sz[ch[r][0]] != x){
		if(x > sz[ch[r][0]]){
			x -= sz[ch[r][0]] + 1;
			r = ch[r][1];
		}
		else{
			r = ch[r][0];
		}
	}
	splay(r, 0);
}
void remove(){
	if(ch[rt][0] == 0){
		rt = ch[rt][1];
	}
	else{
		int y = ch[rt][0];
		while(ch[y][1]){
			y = ch[y][1];
		}
		splay(y, rt);
		ch[y][1] = ch[rt][1];
		pre[ch[rt][1]] = y;
		rt = y;
	}
	pre[rt] = 0;
}
int main(){
	int n, x;
	scanf("%d", &n);
	a[1] = 0;
	for(int i = 2; i <= n; ++i){
		scanf("%d", &a[i]);
	}
	rt = tot = 0;
	newNode(rt, 0);
	for(int i = 2; i <= n; ++i){
		insert(i);
	}
	for(int i = n; i >= 1; --i){
		find(a[i]);
		ans[i] = rt;
		remove();
	}
	for(int i = 1; i <= n; ++i){
		printf("%d\n", ans[i]);
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值