2182

/*
我这种排序的方法是不对的:反例,0 1 0 1 0,最佳答案是 4 5 2 3 1 

正确的方法是从后往前,每次都拿出当前数组的第K大数
*/

// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>

using namespace std;

// typedef
typedef __int64 LL;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)

#define Z(a) (a<<1)
#define Y(a) (a>>1)

const double eps = 1e-6;
const double INFf = 1e100;
const int INFi = 1000000000;
const LL INFll = (LL)1<<62;
const double Pi = acos(-1.0);

template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
	if(x>y) return x;
	return y;
}
template<class T> inline T TMIN(T x,T y)
{
	if(x<y) return x;
	return y;
}
template<class T> inline T MMAX(T x,T y,T z)
{
	return TMAX(TMAX(x,y),z);
}
template<class T> inline T MMIN(T x,T y,T z)
{
	return TMIN(TMIN(x,y),z);
}
template<class T> inline void SWAP(T &x,T &y)
{
	T t = x;
	x = y;
	y = t;
}


// code begin
short Bit[8010];
int Nx;
int N;
short Q[8010];
short ans[8010];

inline int lowBit(int x)
{
	return x&(-x);
}

void update(int x,int c)
{
	for(int i=x;i<Nx;i+=lowBit(i))
	{
		Bit[i]+=c;
	}
}

int getSum(int x)
{
	int ans = 0;
	for(int i=x;i>0;i-=lowBit(i))
	{
		ans += Bit[i];
	}
	return ans;
}

int main()
{
	read;
	write;
	scanf("%d",&N);
	for(int i=2;i<=N;i++)
		scanf("%d",Q+i);
	memset(Bit,0,sizeof(Bit));
	Nx = N+1;
	for(int i=1;i<=N;i++)
		update(i,1);
	Q[1] = 0;
	int l,r,mid,tmp;
	for(int i=N;i>0;i--)
	{
		// Q[i];
		l = 1;
		r = Nx;
		while(l<r)
		{
			mid = Y(l+r);
			tmp = getSum(mid);
			if(tmp>=Q[i]+1)
			{
				r = mid;
			}
			else
			{
				l = mid+1;
			}
		}
		ans[i] = r;
		update(r,-1);
	}
	for(int i=1;i<=N;i++)
	{
		printf("%d\n",ans[i]);
	}
	return 0;
}

posted @ 2011-04-15 09:36  AC2012  阅读(169)  评论(0编辑  收藏  举报