Minimum Inversion Number(归并排序)

Minimum Inversion Number

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

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

题解;题意就是每次把序列的第一个元素放到最后,求最小逆序数;建设当前值为x则,每次逆序数减小x,增加N-1-x;因为x比x个数大,比N-1-X个数小;

代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define MIN(x,y)(x<y?x:y)
 4 const int MAXN=5010;
 5 int a[MAXN],b[MAXN],anser,c[MAXN];
 6 void mergesort(int l,int r,int mid){
 7     int i=l,j=mid+1,k=l;
 8     while(i<=mid&&j<=r){
 9         if(a[i]<=a[j])b[k++]=a[i++];
10         else{
11             anser+=j-k;
12             b[k++]=a[j++];
13         }
14     }
15     while(i<=mid)b[k++]=a[i++];
16     while(j<=r)b[k++]=a[j++];
17     for(i=l;i<=r;i++)a[i]=b[i];
18 }
19 void merge(int l,int r){
20     if(l<r){
21         int mid=(l+r)>>1;
22         merge(l,mid);
23         merge(mid+1,r);
24         mergesort(l,r,mid);
25     }
26 }
27 int main(){
28     int N;
29     while(~scanf("%d",&N)){
30         for(int i=0;i<N;i++)scanf("%d",a+i),c[i]=a[i];
31         anser=0;
32         merge(0,N-1);
33         int temp=anser;
34         //printf("%d\n",anser);
35         for(int i=0;i<N;i++){
36             temp=temp+(N-1-2*c[i]);
37             //printf("%d %d\n",c[i],temp);
38             anser=MIN(anser,temp);
39         }
40         printf("%d\n",anser);
41     }
42     return 0;
43 }
44 //n-a-1-(a) N-2*a+1;
时间: 2024-11-04 08:09:09

Minimum Inversion Number(归并排序)的相关文章

hdu1394 [Minimum Inversion Number]

Minimum Inversion Number 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 e

HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由[1,N]构成,可以通过旋转把第一个移动到最后一个. 问旋转后最小的逆序数对. 分析: 注意,序列是由[1,N]构成的,我们模拟下旋转,总的逆序数对会有规律的变化. 求出初始的逆序数对再循环一遍就行了. 至于求逆序数对,我以前用归并排序解过这道题:点这里. 不过由于数据范围是5000,所以完全可以用线

HDU 1394——Minimum Inversion Number(最小逆序数)

题意: 给定一个序列,里面的数是0到n-1,  每次要把第一个数放到最后一个数,重复n次,求n次操作中最小的逆序数是多少? 思路: 先求出初始的逆序数,然后每移动第一个数到最后面,那么逆序数要减去比它小的数的个数,加上比它大的数的个数. 如果我输入的数是a[i],那么比它小的数的个数就有a[i]个,比它大的数的个数就有n-1-a[i]个 方法:    归并排序 ,树状数组,线段树 归并排序代码: #include<iostream> #include<cstring> #inclu

HDU 1394 Minimum Inversion Number Segment Tree解法

本题有两个考点: 1 求逆序数的性质 计算逆序数的公式, 一个数arr[i]从前面放到后面,必然会有n-arr[i]-1个数比这个大,那么就有n-arr[i]-1个逆序数增加,同时因为前面少了个arr[i]数,那么就必然有arr[i]个(加上零)数比起小的数失去一个逆序数,总共失去arr[i]个逆序数,所以新的逆序数为增加了n-arr[i]-1-arr[i]个逆序数(当然有可能是减小了,视arr[i]的值而定. 2 如何求一个数列的逆序数 可以使用归并排序来求,也可以使用线段树来求.两者都是二分

TOJ1254: Minimum Inversion Number

描述 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 w

Minimum Inversion Number 【线段数】

Problem DescriptionThe 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

hdu 1394 Minimum Inversion Number(这道题改日我要用线段树再做一次哟~)

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

HDU1394 Minimum Inversion Number 线段树+数学

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

HDOJ 1394 Minimum Inversion Number 求循环串的最小逆序数(暴力&amp;&amp;线段树)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14879    Accepted Submission(s): 9082 Problem Description The inversion number of a given number sequence a1, a2, ..., a