Problem I: Inversion Sequence
Time Limit: 2 Sec Memory Limit: 256 MBSubmit: 83 Solved: 22
[ Submit][ Status][ Web Board]
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
HINT
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-6;
const int MS=10005;
int num[MS];
int node[4*MS];
bool flag;
int n;
int leaf;
void init()
{
leaf=1;
flag=true;
while(leaf<=n+1)
leaf<<=1;
memset(node,0,sizeof(node));
for(int i=leaf+1;i<=leaf+n;i++)
node[i]=1;
for(int i=leaf-1;i>0;i--)
node[i]+=node[i<<1]+node[i<<1|1];
}
int insert(int pos)
{
int p=1;
while(p<leaf)
{
node[p]--;
if(node[p]<0)
{
flag=false;
return 1;
}
if(node[p<<1]-pos>0)
p=p+p;
else
{
pos-=node[p<<1];
p=2*p+1;
}
}
node[p]=0;
return p-leaf;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
int x;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(flag)
num[insert(x)]=i;
}
if(flag==false)
printf("No solution");
else
for(int i=1;i<=n;i++)
{
if(i>1)
printf(" ");
printf("%d",num[i]);
}
printf("\n");
}
return 0;
}
本文介绍了一种解决特定逆序数序列问题的方法,通过给定的逆序数序列找到对应的全排列序列,若不存在这样的排列,则输出无解。文章提供了一个完整的C++实现方案,包括段树的数据结构应用。

237

被折叠的 条评论
为什么被折叠?



