zoj1201--------Inversion

来源:互联网 发布:12864显示一个数组 编辑:程序博客网 时间:2024/05/16 13:50
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:
9P 5 9 1 8 2 6 4 7 39I 2 3 6 4 0 2 2 1 00


Sample Output:
2 3 6 4 0 2 2 1 0
5 9 1 8 2 6 4 7 3

 

找到输入数组与输出数组之间的规律即可

P规则是1的前面有几个比他大,写到数组第一个位置,2前边有几个比他大,写到第二个位置,依次类推

I 规则可以这样理解,现有一个空数组都为0,1前面有两个0,写到第三个位置,2前面有三个零,这是第三个位置已经写为1,所以2写到第5个位置,依次~

 

代码如下:

#include <iostream>using namespace std;int main(){   int n,i,j,k,m,count;    while(cin>>n&&n!=0)   {      int A[n];      int B[n];       char c;      for(i=0;i<n;i++)            B[i]=0;cin>>c;       for(i=0;i<n;i++)          cin>>A[i];      if(c=='P')      {          for(i=0;i<n;i++)            {                k=i+1;                count=0;                while(k--)                    if(A[i]<A[k])                        count++;                    B[A[i]-1]=count;            }            for(i=0;i<=n-1;i++)            {                if(i==0)                    cout<<B[i];                else                    cout<<' '<<B[i];            }            cout<<endl;              }      else if(c=='I')      {           for(i=0;i<n;i++)            {                j=0;                 k=i+1;                m=A[i];                 while(m+1)                {                     if(B[j]==0)                        m--;                    j++;                }                 B[j-1]=k;             }            for(i=0;i<=n-1;i++)            {                if(i==0)                    cout<<B[i];                else                    cout<<' '<<B[i];            }            cout<<endl;         }                         }   return 0;  } 


 

 

原创粉丝点击