zoukankan      html  css  js  c++  java
  • hdu1394 Minimum Inversion Number(最小逆序数)

    Minimum Inversion Number

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 1   Accepted Submission(s) : 1

    Font: Times New Roman | Verdana | Georgia

    Font Size:  

    Problem Description

    The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.

    For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:

    a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
    a2, a3, ..., an, a1 (where m = 1)
    a3, a4, ..., an, a1, a2 (where m = 2)
    ...
    an, a1, a2, ..., an-1 (where m = n-1)

    You are asked to write a program to find the minimum inversion number out of the above sequences.

    Input

    The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.

    Output

    For each case, output the minimum inversion number on a single line.

    Sample Input

    10
    1 3 6 9 0 8 5 7 4 2
    

    Sample Output

    16
    

    Author

    CHEN, Gaoli

    Source

    ZOJ Monthly, January 2003

    先百度逆序数:在一个排列中。假设一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。逆序数为偶数的排列称为偶排列;逆序数为奇数的排列称为奇排列

    如2431中,21。43,41,31是逆序,逆序数是4,为偶排列。

    对于这个题求把第一个数放到最后一个数的最小逆序数,对于原序列而言,假设把第一个数放到最后一个数,逆序列添加n-num[0]+1,逆序列降低

    num[0].

    所以这道题:

    #include <stdio.h>
    int main()
    {
    	int num[5005],n;
    	while(scanf("%d",&n)!=EOF)
    	{
    		for(int i=0;i<n;i++)
    		scanf("%d",&num[i]);
    		int sum=0,temp;
    		for(int i=0;i<n;i++)
    		for(int j=i+1;j<n;j++)
    		if(num[i]>num[j]) sum++;
    		temp=sum;
    		for(int i=n-1;i>=0;i--)
    		{
    			temp-=n-1-num[i];
    			temp+=num[i];
    			if(temp<sum)
    			sum=temp;
    		}
    		printf("%d
    ",sum);
    	}
    	return 0;
    }



  • 相关阅读:
    LAB8 android
    python3速查参考- python基础 1 -> python版本选择+第一个小程序
    selenium三种断言以及异常类型
    selenium+java:获取列表中的值
    selenium测试(Java)--元素操作(五)
    redhat网卡设置
    PYTHON TDD学习(一)-->Python 3.4版本环境安装Django及其启动
    渗透测试
    实用--功能测试方法与实际测试内容
    SQL常见面试题(学生表_课程表_总表)
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6789466.html
Copyright © 2011-2022 走看看