POJ2182 Lost Cows(线段树+树状数组)

POJ2182 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
    给一串含n-1个元素数组,例1,2,1,0,给定的数组从第二个位置开始,第一个元素永远为0。所以实际上改数组是0,1,2,1,0。以第一个0为例,其意思是在其之前只有0个比他小的元素,以此类推。此题可以使用线段树,树状数组两种写法。
    线段树:
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<functional> 
#include<map>
using namespace std;
typedef long long ll;
const int N=1e5+10,NN=2e3+10,INF=0x3f3f3f3f;
const ll MOD=1e9+7;
struct Tree{
	int l;
	int r;
	int len;
	int sum;
	int mid(){
		return (l+r)/2;
	}
}tree[N<<2];
int n;
int pre[N],ans[N];
void pushup(int rt){
	tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void buildtree(int left,int right,int rt){
	tree[rt].l=left;
	tree[rt].r=right;
	tree[rt].len=right-left+1;
	if(left==right){
		tree[rt].sum=1;
		return ;
	}
	int mid=tree[rt].mid();
	buildtree(left,mid,rt<<1);
	buildtree(mid+1,right,rt<<1|1);
	pushup(rt);
}
int update(int left,int right,int k,int rt){
	--tree[rt].sum;
	if(left==right) return left;
	int mid=tree[rt].mid();
	if(k<=tree[rt<<1].sum) update(left,mid,k,rt<<1);
	else update(mid+1,right,k-tree[rt<<1].sum,rt<<1|1);
}
int main(){
	scanf("%d",&n);
	pre[1]=0;
	for(int i=2;i<=n;i++) scanf("%d",&pre[i]);
	buildtree(1,n,1);
	for(int i=n;i>=1;i--) ans[i]=update(1,n,pre[i]+1,1);
	for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
	return 0;
}

树状数组:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<functional> 
#include<map>
//#include<unordered_map>
#define lowbit(x) ((x)&-(x));
using namespace std;
typedef long long ll;
const int N=1e6+10,NN=2e3+10,INF=0x3f3f3f3f;
const ll MOD=1e9+7;
int n;
int pre[N],tree[N],ans[N];
void add(int x,int d){
	while(x<=n){
		tree[x]+=d;
		x+=lowbit(x);
	}
}
int sum(int x){
	int sum=0;
	while(x>0){
		sum+=tree[x];
		x-=lowbit(x);
	}
	return sum;
}
int findpos(int x){
	int l=1,r=n;
	while(l<r){
		int mid=(l+r)>>1;
		if(sum(mid)<x) l=mid+1;
		else r=mid;
	}
	return l;
}
void init(){
	
}
int main(){
	scanf("%d",&n);
	pre[1]=0;
	for(int i=2;i<=n;i++) scanf("%d",&pre[i]);
	for(int i=1;i<=n;i++) tree[i]=lowbit(i);
	for(int i=n;i>0;i--){
		int x=findpos(pre[i]+1);
		add(x,-1);
		ans[i]=x;
	}
	for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
}

猜你喜欢

转载自blog.csdn.net/Hc_Soap/article/details/107463477
今日推荐