• hdu1394


    Minimum Inversion Number

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


    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
     

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    int a[5005];
    struct Node
    {
        int l,r,num;
    } tree[50000];
    void Build(int i,int l,int r)
    {
        tree[i].l = l;
        tree[i].r = r;
        tree[i].num = 0;
        if(l == r)
        {
            return;
        }
        int mid = (l+ r) / 2;
        Build(2*i,l,mid);
        Build(2*i+1,mid+1,r);
    }

    void updata(int i,int x)
    {
        int l = tree[i].l;
        int r = tree[i].r;
        int mid = (l + r) / 2;
        if(x == l && x == r)
        {
            tree[i].num ++;
            return;
        }
        if(x <= mid)
            updata(2*i,x);
        else
            updata(2*i+1,x);
        tree[i].num = tree[2*i].num + tree[2*i+1].num;
    }

    int Query(int n,int x,int y)
    {
        int l = tree[n].l;
        int r = tree[n].r;
        int mid = (l + r) / 2;
        int ans = 0;
        if(x == l && y == r)
            return tree[n].num;
        if(y<= mid)
            ans += Query(2*n,x,y);
        else if(x> mid)
            ans += Query(2*n+1,x,y);
        else
            ans+=Query(2*n,x,mid)+Query(2*n+1,mid+1,y);
        return ans;
    }
    int main()
    {
        int n,sum,ans;
        int i,j;
        while(scanf("%d",&n) != EOF)
        {
            sum = 0;
            Build(1,0,n);
            for(i = 1; i <= n; i++)
            {
                scanf("%d",&a[i]);
                updata(1,a[i]);
                sum += Query(1,a[i]+1,n);
            }
            ans = sum;
            for(i = 1; i <=n; i++)
            {
                sum = sum + (n - 1 - a[i]) - a[i];
                ans=min(ans,sum);
            }
            printf("%d ",ans);
        }
        return 0;
    }

  • 相关阅读:
    PHP文件打开、关闭、写入的判断与执行
    统计文件大小,以GB、MB、KB、B输出
    超强功能file_put_contents()函数(集成了fopen、fwrite、fclose)
    fputcsv命令,写csv文件,遇到的小问题(多维数组连接符)
    Rename 更改文件、文件夹名称
    PHP学习——定界符格式引起的错误
    SPOJ 1873 Accumulate Cargo
    POJ 3657 Haybale Guessing
    HDU 1512 Monkey King
    POJ 1741 Tree
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3389616.html
Copyright © 2020-2023  润新知