ZOJ_1201_Inversion

Inversion

Time Limit: 2 Seconds       Memory Limit: 65536 KB

  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

题意和思路:首先要了解题目的意思,这个重点的翻译在我程序的注释里面,P的情况比较简单不说了,关键是已知inversion table如何求permutation的问题,这个关键是找规律,寻求思路的方法可以重纸上自己尝试写下来仔细挖掘,然后可以假设inversion table的值就是要插入的位置,这样B[J]中的J就是具体插入的值,这样符合题意,这时候注意小细节 要从后面开始处理,为什么不从前面开始处理呢,因为这样插入的时候将后面的值后移就不符合题意了,具体的变化可自己在纸上演绎下 情况就知道了。

哎 网上说这是水题。。我花了好几个小时。。果然是太菜了

Sample Program Here
/******************************************
**
**      Author: Wan KaiMing
**      Date:   2012-10-29-15.59 星期一
**
*******************************************/

/*
   最重要的翻译主要是这几句
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

翻译:排列{A1,A2,...,An}的倒置表B1,B2,...,Bn是通过使Bj为元素j左边大于j的
数量之和(也就是说Bj是那些第二个元素为j的倒置数的个数)

那么以上的举例也可以明白B1,B2此时的j=1和j=2;
当j=1时,此时元素1左边有2个数5和9他们的倒置数为(5,1) (9,1)  其他的倒置数不看
因为就这两个的第二个元素为j
*/

#include<iostream>
#include<string>
using namespace std;

//统计该位置左侧的倒置数的个数,用于为'P'的时候
int sum(int a[],int value,int index){
  int s=0;//统计个数
  for(int i=0;i<index;i++){
    if(a[i]>value) s++;
  }

  return s;
}

//用于为'I'的时候 进行插入操作,后面的位移
void insert(int b[],int n,int value,int index){
    if(b[index]==0) b[index]=value;
    else{
      for(int i=n-1;i>=index;i--){  //index开始的每个元素后移一格
           b[i+1]=b[i];
      }
      b[index]=value;

    }
}

int main(){
   int a[51],n; //a[50]用于保存permutation或者Inversion,n表示数字个数
   char ch; //判断是排列还是倒置表即使P or I
   int i,j;
   while(1){
      cin>>n;
      if(n==0) break;
      int b[51]={0};//用于保存处理结果,为I或P,初始化全为0
      cin>>ch;
      //初始化数据
      for(i=0;i<n;i++) cin>>a[i];

      if(ch=='P'){
         for(i=1;i<=n;i++){  //注意范围是1-50所以从1开始
           for(j=0;j<n;j++){ //循环 找到元素值为i的元素在排列中的位置
             if(a[j]==i) b[i]=sum(a,i,j);  //i是值,j是位置
           }
         }

         //输出处理结果
         for(int i=1;i<=n;i++){
             if(i!=n)
             cout<<b[i]<<' ';
             else
             cout<<b[i]<<endl;

         }
      }
      else{
         for(i=n;i>0;i--){  //从后面的开始处理,a[i]的值就是插入的位置,从1-50
            insert(b,n,i,a[i-1]);//b是要插入的数组,n是数组大小,i是具体插入的值,a[i]是插入位置
         }


        //输出处理结果
         for(int i=0;i<n;i++){
             if(i!=n-1)
             cout<<b[i]<<' ';
             else
             cout<<b[i]<<endl;

         }
      }
   }
   return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值