Let { A1,A2,...,An } be a permutation of the set{ 1,2,..., n}. If i < j and Ai > Aj then the pair (Ai,Aj) is called an "inversion" of the permutation. For example, the permutation {3, 1, 4, 2} has three inversions: (3,1), (3,2) and (4,2).
The inversion table B1,B2,...,Bn of the permutation { A1,A2,...,An } is obtained by letting Bj be the number of elements to the left of j that are greater than j. (In other words, Bj is the number of inversions whose second component is j.) For example, the permutation:
{ 5,9,1,8,2,6,4,7,3 }
has the inversion table
2 3 6 4 0 2 2 1 0
since there are 2 numbers, 5 and 9, to the left of 1; 3 numbers, 5, 9 and 8, to the left of 2; etc.
Perhaps the most important fact about inversions is Marshall Hall's observation that an inversion table uniquely determines the corresponding permutation. So your task is to convert a permutation to its inversion table, or vise versa, to convert from an inversion table to the corresponding permutation.
Input:
The input consists of several test cases. Each test case contains two lines.
The first line contains a single integer N ( 1 <= N <= 50) which indicates the number of elements in the permutation/invertion table.
The second line begins with a single charactor either 'P', meaning that the next N integers form a permutation, or 'I', meaning that the next N integers form an inversion table.
Following are N integers, separated by spaces. The input is terminated by a line contains N=0.
Output:
For each case of the input output a line of intergers, seperated by a single space (no space at the end of the line). If the input is a permutation, your output will be the corresponding inversion table; if the input is an inversion table, your output will be the corresponding permutation.
Sample Input:
9 P 5 9 1 8 2 6 4 7 3 9 I 2 3 6 4 0 2 2 1 0 0
Sample Output:
2 3 6 4 0 2 2 1 0 5 9 1 8 2 6 4 7 3
题意:题意就是1前面有几个数比1大,2前面有几个数比2大...n前面有几个数比n大,然后按顺序输出个数,或
已知道个数推出之前的排列
思路:P进去很简单,只要比较然后记个数即可,I进去比较麻烦
I进来的做法:我们用a[i]来接收输入序列,b[j]来接收输出序列,我们就举个例子首先b数组全置0,a[0]进来=2说明1前有2个数比1大,而此时
b[0]=0,所以说明第1个位置没人放,而a[0]不等于0(a[0]==0说明没有数比1大,就是说明a[0]==0时,b[j]这个位置就是1所要放的位置),
说明我们还没找到1所要找的位置,所以1不能放在b[0],所以我们a[0]--,j++,继续比较第2个位置,直到第3个位置时(j==2),a[0]==0,所以1放在
b[2]里。然后a[1]=3进来,说明2前有3个数比2大,同上,但是由于b[2]已经被1存入,所以这时j要多加1次。
代码:
#include <stdio.h>
int main()
{
int N,n,k;
int a[100],b[100]={0};
char pior;
int i,j;
scanf("%d",&N);
getchar();
while(N!=0)
{
pior=getchar();
for(i=0;i<N;i++)
scanf("%d",&a[i]);
if(pior=='P')
{
k=0;
for(i=1;i<=N;i++)
{
for(j=0;j<N;j++)
{
if(a[j]==i)
n=j;
}
for(j=0;j<n;j++)
{
if(a[j]>i)
k++;
}
printf("%d",k);
if(i!=N)
printf(" ");
else
printf("\n");
k=0;
}
}
else if(pior=='I')
{
for(i=0;i<N;i++) b[i]=0;
for(i=1;i<=N;i++)
{
for(j=0;;j++)
{
if(!b[j])
{
if(!a[i-1])
{
b[j]=i;
break;
}
else
a[i-1]--;
}
}
}
for(i=0;i<N;i++)
{
printf("%d",b[i]);
if(i!=N-1)
printf(" ");
else printf("\n");
}
}
scanf("%d",&N);
getchar();
}
return 0;
}
int main()
{
int N,n,k;
int a[100],b[100]={0};
char pior;
int i,j;
scanf("%d",&N);
getchar();
while(N!=0)
{
pior=getchar();
for(i=0;i<N;i++)
scanf("%d",&a[i]);
if(pior=='P')
{
k=0;
for(i=1;i<=N;i++)
{
for(j=0;j<N;j++)
{
if(a[j]==i)
n=j;
}
for(j=0;j<n;j++)
{
if(a[j]>i)
k++;
}
printf("%d",k);
if(i!=N)
printf(" ");
else
printf("\n");
k=0;
}
}
else if(pior=='I')
{
for(i=0;i<N;i++) b[i]=0;
for(i=1;i<=N;i++)
{
for(j=0;;j++)
{
if(!b[j])
{
if(!a[i-1])
{
b[j]=i;
break;
}
else
a[i-1]--;
}
}
}
for(i=0;i<N;i++)
{
printf("%d",b[i]);
if(i!=N-1)
printf(" ");
else printf("\n");
}
}
scanf("%d",&N);
getchar();
}
return 0;
}