您的位置:首页 > 其它

uva 11525(线段树)

2013-09-08 23:42 288 查看
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2520

题意:有一个排列1~k,求第n个排列,其中n为

#include<stdio.h>
#include<string.h>
const int maxn=50000+10;
int c[maxn],num[maxn];
int n;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int num)
{
while(x<=n)
{
c[x]+=num;
x+=lowbit(x);
}
}
int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
int binary(int x)
{
int low=1,high=n;
while(low<=high)
{
int m=(low+high)>>1;
int cnt=sum(m);
if(cnt==x)
{
if(num[m])
return m;
else
high=m-1;
}
else if(cnt<x)
low=m+1;
else
high=m-1;
}
return 0;
}
int main()
{
int t,i,x;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(c,0,sizeof(c));
for(i=1;i<=n;i++)
num[i]=1;
for(i=1;i<=n;i++)
update(i,1);
for(i=0;i<n;i++)
{
scanf("%d",&x);
int cnt=binary(x+1);
update(cnt,-1);
num[cnt]=0;
printf("%d",cnt);
if(i!=n-1)
printf(" ");
}
printf("\n");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: