HDU1394-Minimum Inversion Number-归并排序求最小逆序数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394

理解了归并排序,学习了一下别人的博客,有把这个题目给敲了一遍。。。

这道题我用归并排序是我目前过的最快的算法62ms。。。当然,在我得博客里也有有线段树和树状数组求逆序数的模板;

这里我就没有离散化,还不能算是所有逆序数的模板。。。

对于归并排序不太了解的我推荐一个博客,里面有很详细的图解;链接:http://www.cnblogs.com/jillzhang/archive/2007/09/16/894936.html

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<cmath>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#define LL long long
#define inf 1<<30
#define s(a) scanf("%d",&a)
#define Clear(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=200005;
int n,ans;
int a[N],b[N];
void Merge_Array(int l,int mid,int r)   //  二路合并;
{
    int i=l,j=mid+1,k=0;
    int *p;
    p=(int *)malloc((r-l+1)*sizeof(int));
    while(i<=mid&&j<=r){
        if(a[i]<=a[j]){
            p[k++]=a[i++];
        }else{
            p[k++]=a[j++];
            ans+=mid-i+1;
        }
    }
    while(i<=mid){
        p[k++]=a[i++];
    }
    while(j<=r){
        p[k++]=a[j++];
    }
    int cnt=0;
    for(int i=l;i<=r;i++){
        a[i]=p[cnt++];
    }
    free(p);
}
void Merge_Sort(int l,int r)    //  分治法,调用递归;
{
    if(l<r){
        int mid=(l+r)>>1;
        Merge_Sort(l,mid);
        Merge_Sort(mid+1,r);
        Merge_Array(l,mid,r);
    }
}
int main()
{
    while(~scanf("%d",&n)){
        ans=0;
        for(int i=0;i<n;i++){s(a[i]);b[i]=a[i];}
        Merge_Sort(0,n-1);      //  从0到n-1排序;
        int cnt=ans;
        for(int i=0;i<n;i++){
            ans=ans-b[i]+n-1-b[i];
            if(cnt>ans)  cnt=ans;
        }
        printf("%d\n",cnt);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-06 19:33:25

HDU1394-Minimum Inversion Number-归并排序求最小逆序数的相关文章

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

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

HDU1394 Minimum Inversion Number 【线段树】+【逆序数】

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

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

HDU1394 线段树求最小逆序数

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1394 求最小的逆序数,在此贴下逆序数的概念: 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数.逆序数为偶数的排列称为偶排列:逆序数为奇数的排列称为奇排列.如2431中,21,43,41,31是逆序,逆序数是4,为偶排列. 也是就说,对于n个不同的元素,先规定各元素之间有一个标准次序(例如n个 不同的自然数,可规

【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)

Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, arrays may be too large for us to wait around for insertion sort to finish. Is there some other way we can calculate the number of times Insertion Sort

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

hdu 1394 Minimum Inversion Number 归并求逆序数

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

hdu1394Minimum Inversion Number(线段树,求最小逆序数)

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

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

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