您的位置:首页 > 其它

HDU - 1394 _Minimum Inversion Number(归并排序求逆序数)

2017-10-29 16:29 411 查看

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

题意:数组 0-(n-1),每次把第一个元素放到最后一个,求所有的排列中逆序数的最小值

即:

数组a[]:其根据题意移动产生的序列有

3 4 1 2 0 逆序数:8

4 1 2 0 3 逆序数:6

1 2 0 3 4 逆序数:2

2 0 3 4 1 逆序数:4

所以最小逆序数为2

分析:先利用递归排序把起初的逆序数个数解出来,在利用递推—->>>如果求出第一种情况的逆序列,其他的可以通过递推来求出来,一开始是b[0],b[1],b[2]….b[n-1]它的逆序列个数是N个,如果把b[0]放到b[n-1]后面,逆序列个数会减少b[0]个,相应会增加N-(b[0]+1)个。

先求解一开始的数组的逆序数个数,在此基础上再进行加减操作,根据递归操作,得出最小的逆序数个数。

代码:

#include <iostream>
#include <cstdio>
using namespace std;
int n;
int inf = 99999999;
int a[500005],b[500005];
int L[500005],R[500005];
int Merge(int a[],int n,int left,int mid,int right)
{
int i,j,k;
int cnt = 0;
int n1 = mid-left;
int n2 = right-mid;
for(i=0;i<n1;i++)
L[i] = a[left+i];
for(j=0;j<n2;j++)
R[j] = a[mid+j];
L[n1] = R[n2] = inf;
i = j = 0;
for(k=left;k<right;k++)
{
if(L[i] <= R[j])
{
a[k] = L[i++];
}
else
{
a[k] = R[j++];
cnt = cnt + n1-i;  //只要左边第 i个元素大于右边第 j 个元素,则左边剩下的元素一定大于右边第 j 个元素,所以直接把他们都加了就好
}
}
return cnt;
}
int MergeSort(int a[],int n,int left,int right)
{
int mid;
int u,v,w;
if(left+1 < right)
{
mid = (left+right)/2;
u = MergeSort(a,n,left,mid);
v = MergeSort(a,n,mid,right);
w = Merge(a,n,left,mid,right);
return u+v+w;   //
}
else
return 0;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int ans = 0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[i] = a[i];
}

ans = MergeSort(a,n,0,n);

//printf("%d\n",ans);
int minx = inf;
if(ans < minx)
minx = ans;
for(int i=0;i<n;i++)
{
ans = ans - b[i] + n - 1 - b[i];
if(minx > ans )
minx = ans;
}
printf("%d\n",minx);
}
return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int ans;
int inf = 99999999;
int a[5050],b[5050];

void Merge(int left,int mid,int right)
{
int i,j,cnt=0;
int *p;
i = left;
j = mid + 1;
p = (int *)malloc(sizeof(int )*(right-left+1));
while(i <= mid && j <= right)
{
if(a[i] <= a[j])
{
p[cnt++] = a[i];
i++;
}
else
{
p[cnt++] = a[j];
j++;
ans = ans + mid - i + 1;
}
}
while(i<=mid)
{
p[cnt++] = a[i++];
}
while(j<=right)
{
p[cnt++] = a[j++];
}
cnt = 0;
for(i=left;i<=right;i++)
a[i] = p[cnt++];
free(p);
}

void MergeSort(int left,int right)
{
if(left < right)
{
int mid = (left + right)/2;
MergeSort(left,mid);
MergeSort(mid+1,right);
Merge(left,mid,right);
}
}

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[i] = a[i];
}
ans = 0;
MergeSort(0,n-1);
int minx = inf;
if(ans < minx)
minx = ans;
for(int i=0;i<n;i++)
{
ans = ans - b[i] + n - 1 - b[i];
if(minx > ans )
minx = ans;
}
printf("%d\n",minx);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: