zoukankan      html  css  js  c++  java
  • 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): 6365    Accepted Submission(s): 3875


    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
     

    --------------------------------------------------

    看不懂怎么证的orz

    若abcde...的逆序数为k,那么bcde...a的逆序数是多少?我们假设abcde...中小于a的个数为t , 那么大于a的个数就是n-t-1,当把a移动最右位时,原来比a

    大的现在都成了a的逆序对,即逆序数增加n-t-1,但是原来比a小的构成逆序对的数,现在都变成了顺序,因此逆序对减少t ,所以新序列的逆序数为 k +=

    n - t - t -1,即k += n-1-2 * t , 于是我们只要不断移位(n次),然后更新最小值就可以了

    -------------------------------------------------

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #define N 50005
    using namespace std;
    int num[N];
    struct Tree
    {
        int l;
        int r;
        int sum;
    } tree[N*4];
    void build(int root,int l,int r)
    {
        tree[root].l=l;
        tree[root].r=r;
        if(tree[root].l==tree[root].r)
        {
            tree[root].sum=0;
            return;
        }
        int mid=(l+r)/2;
        build(root<<1,l,mid);
        build(root<<1|1,mid+1,r);
        tree[root].sum = tree[root<<1].sum+ tree[root<<1|1].sum;
    }
    void update(int root,int pos,int val)
    {
        if(tree[root].l==tree[root].r)
        {
            tree[root].sum=val;
            return;
        }
        int mid=(tree[root].l+ tree[root].r)/2;
        if(pos<=mid)
            update(root<<1,pos,val);
        else
            update(root<<1|1,pos,val);
        tree[root].sum = tree[root<<1].sum+ tree[root<<1|1].sum;
    }
    int query(int root,int L,int R)
    {
        if(L<=tree[root].l&&R>=tree[root].r)
            return tree[root].sum;
        int mid=(tree[root].l+ tree[root].r)/2,ret=0;
        if(L<=mid) ret+=query(root<<1,L,R);
        if(R>mid) ret+=query(root<<1|1,L,R);
        return ret;
    }
    
    int x[N];
    int main()
    {
        int n;
        int sum,ret;
        while (~scanf("%d",&n))
        {
            build(1,0,n-1);
            sum=0;
            for (int i=0;i<n;i++)
            {
                scanf("%d",&x[i]);
                sum+=query(1,x[i],n-1);
                update(1,x[i],1);
            }
            ret=sum;
            for (int i=0;i<n;i++)
            {
                sum+=n-x[i]-x[i]-1;
                if (sum<ret)
                {
                    ret=sum;
                }
            }
            printf("%d\n",ret);
        }
        return 0;
    }
    







  • 相关阅读:
    多项式计算
    递归算法
    递推算法
    穷举算法
    两个数用二进制表示,有多少位不同
    一个整数的二进制数中1的个数
    将十进制数转化为二进制数
    (调用方法)判断一个整数是否为素数两种方法,年份是否是闰年,交换两个数值
    快速排序算法
    用户登录系统
  • 原文地址:https://www.cnblogs.com/cyendra/p/3038416.html
Copyright © 2011-2022 走看看