• 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): 3973    Accepted Submission(s): 2380


    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
     
    Recommend
    Ignatius.L

    //线段树基础题,求逆序数、应该没树状数组快

    //这题可以改难点、就是这些数是不连续的、、、离散化、、

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #define lson l,m,k<<1
    #define rson m+1,r,k<<1|1
    #define N 5003
    using namespace std;
    int st[N<<2];
    void up(int &k)
    {
        st[k]=st[k<<1]+st[k<<1|1];
    }
    void build(int l,int r,int k)
    {
        if(l==r)
        {
            st[k]=0;
            return ;
        }
        int m=(l+r)>>1;
        build(lson);
        build(rson);
        up(k);
    }
    void updata(int &index,int l,int r,int k)
    {
        if(l==r)
        {
            st[k]=1;
            return ;
        }
        int m=(l+r)>>1;
        if(index<=m) updata(index,lson);
        else updata(index,rson);
        up(k);
    }
    int query(int &L,int &R,int l,int r,int k)
    {
        if(L<=l&&R>=r)
        {
            return st[k];
        }
        int m=(l+r)>>1,te=0;
        if(L<=m) te+=query(L,R,lson);
        if(R>m) te+=query(L,R,rson);
        return te;
    }
    int re[N];
    int main()
    {
        int n,m;
        int sum;
        while(scanf("%d",&n)!=EOF)
        {   n--;
            build(0,n,1);
            sum=m=0;
            for(int i=0;i<=n;i++)
             {
                 scanf("%d",&re[i]);
                 sum+=i-query(m,re[i],0,n,1);
                 updata(re[i],0,n,1);
             }
             m=sum;
            for(int i=0;i<=n;i++)
            {
                sum=sum+n-re[i]-re[i];
                if(sum<m)
                 m=sum;
            }
            printf("%d\n",m);
        }
        return 0;
    }
    // 下面是数组数组代码,果然更快,归并排序应该也很快的

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #define N 5003
    int tree[N];
    int n;
    int lb(int x){return x&-x;}
    void up(int i)
    {
        for(;i<=n;tree[i]+=1,i+=lb(i));
    }
    int gs(int i)
    {
        int sum=0;
        for(;i>0;sum+=tree[i],i-=lb(i));
        return sum;
    }
    int main()
    {
        int a[N];
        int i,sum,min;
        while(scanf("%d",&n)!=EOF)
        {   memset(tree,0,sizeof(tree));
            sum=0;
            for(i=1;i<=n;i++)
               {scanf("%d",&a[i]);
                up(a[i]+1);
                sum+=i-gs(a[i]+1);}
            min=sum;
            for(i=n;i>1;i--)
            {
                sum=sum+a[i]-(n-a[i]-1);
                min=min>sum?sum:min;
            }
            printf("%d\n",min);
        }
        return 0;
    }
  • 相关阅读:
    优酷kux转mp4
    C++实现将一个文件夹内容拷贝至另一个文件夹
    获取NX一组属性
    获取NX特征名称(无时间戳)
    利用glog打印日志
    C++获取运行程序当前目录
    获取NX装配结构信息
    解析形如(k,v)(k,v)(k,v)字符串
    多NX如何共存
    C++ (C#)实现获取NX PART预览图
  • 原文地址:https://www.cnblogs.com/372465774y/p/2596451.html
Copyright © 2020-2023  润新知