hdu 1320 Inversion

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=1320

题目描述:

Inversion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 443    Accepted Submission(s): 297


Problem Description
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. 
 

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


题意:

inversion序列是根据permutation序列中这个数的左边有多少个数大于这个数在permutation序列中的数量。根据这条规则相互转换两个序列。

题解:

1、permutation 转 Inversion : 按照从1到N的顺序扫描permutation序列,在指针未遇到制定数之前,大于这个数的记录数量+1,遇到指定数后退出,然后在指定数的索引处的inversion序列中填入相应的数量的值。
2、Inversion 转 permutation : 根据Inversion的数量,扫描permutation序列(我初始化的时候,这个序列每个元素都是无穷大,这点比较关键),如果permutation 序列大于 Inversion的索引数,那么记录数量+1,当数量==Inversion索引所对应的value的数量值,说明Inversion的索引值应该填到permutation的索引的value的地方,并且这个地方还必须是未填入的值,如果是已经填入的值,则继续挪动permutation的索引指针,直到找到未填如的value位置。详细逻辑见代码。
代码:
#include<stdio.h>
int p_arr[50+5],i_arr[50+5];
int N=0;
char kind='\0';
int main()
{
    while(scanf("%d",&N)!=EOF&&N>0)
    {
        for(int i=0;i<=50+5-1;i++) {p_arr[i]=i_arr[i]=1000;}
        scanf("%c",&kind);
        while(!(kind=='P'||kind=='I')) scanf("%c",&kind);
        if(kind=='I')
        {
            for(int i=0;i<=N-1;i++)
            {
                scanf("%d",&i_arr[i]);
                int bigger_cnt = 0;
                int p_ind=0;
                while(bigger_cnt < i_arr[i]&&p_ind<=N-1)
                {
                    if(p_arr[p_ind]>i+1) bigger_cnt++;
                    p_ind++;
                }
                while(p_arr[p_ind]<=N&&p_ind<=N-1) p_ind++;//it must be not visited
                p_arr[p_ind]=i+1;
            }
            printf("%d",p_arr[0]);
            for(int i=1;i<=N-1;i++) printf(" %d",p_arr[i]);
        }
        else
        {
            for(int i=0;i<=N-1;i++) scanf("%d",&p_arr[i]);
            for(int i=1;i<=N;i++)
            {
                int j=0,bigger_cnt=0;
                while(p_arr[j]!=i&&j<=N-1)
                {
                    if(p_arr[j]>i) bigger_cnt++;
                    j++;
                }
                i_arr[i-1]=bigger_cnt;
            }
            printf("%d",i_arr[0]);
            for(int i=1;i<=N-1;i++) printf(" %d",i_arr[i]);
        }
        printf("\n");
    }
    return(0);
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值