poj2182(二分+树状数组)

Lost Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8341   Accepted: 5320

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
 
        本题给定一个序列a[],a[i]表示比第i+1个位置的元素小的且在其前面的元素个数,要求该序列对应的大小序号。
        本体可以倒着推,每当求当前剩下的最后一个未确定的位置时,我们知道它后面比它小的元素以及它前面比它小的元素,又有规律:前面比它小的元素个数a[]+后面比它小的元素个数=它的对应的大小序号。我们可以枚举元素的大小序号,然后比较是否满足规律的。枚举时若直接枚举,可能会超时;自然想到二分法,本题是满足单调性的。在求比它大的元素个数是可以想到是求对应区间[1,i]的元素个数和,自然想到高效的线段树或是树状数组。
        下面给出树状数组的做法。二分+树状数组。由于有n个数据,每个数据要进行二分查找,且在二分中要用树状数组的查询操作作为判断条件,所以总的时间复杂度为O(N*log^2(N)),这是可以接受的。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

const int MAXN=8000+100;
int n;
int C[MAXN],Lowbit[MAXN],data[MAXN],ans[MAXN];
//*****************************************************************
//C[i] = a[i-lowbit(i)+1] + …+ a[i],下表从1开始
//Lowbit[i]=i&(i^(i-1));或Lowbit[i]=i&(-i); 
//1.查询
int QuerySum(int p)
//查询原数组中下标1-p的元素的和 
{ 
   int nSum=0; 
   while(p>0) 
   {  
      nSum+=C[p]; 
      p-=Lowbit[p]; 
   } 
    return nSum; 
} 

//2.修改+初始化
void Modify(int p,int val) 
//原数组中下表为p的元素+val,导致C[]数组中部分元素值的改变
{ 
    while(p<=MAXN-10) 
   { 
      C[p]+=val; 
      p+=Lowbit[p]; 
    } 
} 
//****************************************************************

//二分枚举返回当前满足条件的最小下表
int Binary_Find(int a)
{
	int low=1,high=n,mid;
	while(low<=high)
	{
		mid=(low+high)>>1;
		if(mid-QuerySum(mid)<a)
			low=mid+1;
		else high=mid-1;
	}
	return low;
}


int main()
{
	int i;
	for(i=1;i<MAXN;i++)
		Lowbit[i]=i&(-i);
	while(~scanf("%d",&n))
	{
		memset(C,0,sizeof(C));
		data[0]=0;
		for(i=1;i<n;i++)
			scanf("%d",&data[i]);
		for(i=n-1;i>=0;i--)
		{
			ans[i]=Binary_Find(data[i]+1);
			Modify(ans[i],1);
		}
		for(i=0;i<n;i++)
			printf("%d\n",ans[i]);
	}
	return 0;
}

     
 

你可能感兴趣的:(数据结构,线段树,树状数组)