您的位置:首页 > 其它

CSU 1555(逆序数复原)

2015-12-11 00:01 369 查看
I - I
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%lld
& %llu
Submit Status Practice CSU
1555

Description

For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence which are prior to j and greater to j at the same time. The sequence a1, a2, a3, … , aN is referred to as the inversion sequence of the original sequence (i1, i2, i3,
… , iN). For example, sequence 1, 2, 0, 1, 0 is the inversion sequence of sequence 3, 1, 5, 2, 4. Your task is to find a full permutation of 1~N that is an original sequence of a given inversion sequence. If there is no permutation meets the conditions please
output “No solution”.

Input

There are several test cases.

Each test case contains 1 positive integers N in the first line.(1 ≤ N ≤ 10000).

Followed in the next line is an inversion sequence a1, a2, a3, … , aN (0 ≤ aj < N)

The input will finish with the end of file.

Output

For each case, please output the permutation of 1~N in one line. If there is no permutation meets the conditions, please output “No solution”.

Sample Input

5
1 2 0 1 0
3
0 0 0
2
1 1


Sample Output

3 1 5 2 4
1 2 3
No solution


题解:找规律,查了题解才知道规律,插入第i个数字,那么就去查找有没有a[i]+1个空位,如果有那么就插入到那个位置,否则就无解,使用线段树维护空位的个数,即区间和查询

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 20000
struct point
{
int lc,rc;
int sum;
}tree[N<<2];
int a
,ans
;
void Pushup(int rt)
{
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void build(int L,int R,int rt)
{
tree[rt].lc=L;
tree[rt].rc=R;
if(L==R)
{
tree[rt].sum=1;
return ;
}
int mid=(L+R)>>1;
build(L,mid,rt<<1);
build(mid+1,R,rt<<1|1);
Pushup(rt);
}
int query(int rt,int x)
{
if(tree[rt].lc==tree[rt].rc)
{
tree[rt].sum=0;
return tree[rt].lc;
}
int ret;
if(tree[rt<<1].sum>=x)
{
ret= query(rt<<1,x);
}
else
{
ret=query((rt<<1|1),(x-tree[rt<<1].sum));
}
Pushup(rt);
return ret;
}
int main()
{
#ifdef CDZSC
freopen("i.txt","r",stdin);
#endif
int n;
while(~scanf("%d",&n))
{
int ok=1;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,n,1);
for(int i=1;i<=n;i++)
{
if(tree[1].sum<a[i]+1)
{
ok=0;
break;
}
else
{
ans[query(1,a[i]+1)]=i;
}
}
if(!ok)
{
puts("No solution");
}
else
{
for(int i=1;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans
);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: