zoukankan      html  css  js  c++  java
  • HDU-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 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

     

     

     

     

     

     

    先百科一下逆序数的概念: 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个 逆序 。一个排列中逆序的总数就称为这个排列的 逆序数 。逆序数为 偶数 的排列称为 偶排列 ;逆序数为奇数的排列称为奇排列 。如2431中,21,43,41,31是逆序,逆序数是4,为偶排列。

    逆序数计算方法是:在逐个元素读取原始数列时,每次读取都要查询当前已读取元素中大于当前元素的个数,加到sum里,最后得到的sum即为原始数列的逆序数。

     

    现在来说一下求解思路:

    输入数组时,每输入一个数,就for(j=0;j<i-1;j++)比较大小,这样用sum把逆序数统计出来,其实这里才是暴力,至于后面的就很巧妙,公式很容易推出,因为题目总是把第一个数移到最后一个位置,所以原来比它小的数(和它构成逆序)在移动之后就不是逆序了,而原来比它大的数(不和它构成逆序)在移动之后就是逆序了,这样sum就变化了:Sum=sum-(low[a[i]])+(up[a[i]]); 显然在序列0,1,2,…..n-1中比a[i]小的数的个数是 Low[a[i]]=a[i];  比a[i]大的数的个数是up[a[i]]=n-a[i]-1; 题目要求是循环移动n次,那么只要写个for,把a[0],a[1],a[2]……a[n-1]都移动一遍,sum进行n次上面的公式运算,同时记录最小值,就是最小逆序数了。

     

    接下来是本馒头暴力AC的代码,数据为5000时的耗时为109ms,应该庆幸数据小= =

     

    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    #define INF 0x3f3f3f3f
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    
    const int MX = 5050;
    int num[MX];
    int n;
    
    int main() {
        //freopen("input.txt", "r", stdin);
        while (scanf("%d", &n) != EOF) {
    
            int sum = 0;
            for (int i = 0; i < n; i++) {
                scanf("%d", &num[i]);
    
                for (int j = 0; j < i; j++) {
                    if (num[j] > num[i]) sum++;
                }
            }
            int Min = INF;
            for (int i = 0; i < n; i++) {
    
                sum = sum - num[i] + n - num[i] - 1;
                Min = min(sum, Min);
            }
    
            printf("%d
    ", Min);
        }
        return 0;
    }


    由于最近在学习线段树,所以这道题也用线段树优化一下,主要是优化算出sum的那一段,使得整体算法时间复杂度减低到nlongn,对数默认以二为底,速度优化到62ms,以下是AC代码:

    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    #define INF 0x3f3f3f3f
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    
    const int MX = 5050;
    int num[MX];
    int n;
    int sum[MX<<2];
    
    void push_up(int rt) {
        sum[rt] = sum[rt<<1] + sum[rt<<1|1];
    }
    void build(int l, int r, int rt) {
        if (l == r) {
            sum[rt] = 0;
            return ;
        }
        int m = (l + r)>>1;
        build(lson);
        build(rson);
        push_up(rt);
    }
    void update(int pos, int l, int r, int rt) {
        if (l == r) {
            sum[rt]++;
            return ;
        }
        int m = (l + r)>>1;
        if (pos <= m) update(pos, lson);
        else update(pos, rson);
        push_up(rt);
    }
    int query(int L, int R, int l, int r, int rt) {
        if (L <= l && r <= R) {
            return sum[rt];
        }
        int m = (l + r)>>1;
        int ret = 0;
        if (L <= m) ret += query(L, R, lson);
        if (R > m) ret += query(L, R, rson);
        return ret;
    }
    int main() {
        //freopen("input.txt", "r", stdin);
        while (scanf("%d", &n) != EOF) {
            memset(sum, 0, sizeof(sum));
            int s = 0;
            build(1, n, 1);
            for (int i = 0; i < n; i++) {
                scanf("%d", &num[i]);
                update(num[i] + 1, 1, n, 1);
                s += query(num[i] + 2, n, 1, n, 1);
            }
            int Min = INF;
            for (int i = 0; i < n; i++) {
    
                s = s - num[i] + n - num[i] - 1;
                Min = min(s, Min);
            }
    
            printf("%d
    ", Min);
        }
        return 0;
    }
    想起来再慢慢看,先写点东西堆在这里,嘻嘻
  • 相关阅读:
    JZ2440开发板开发环境搭建
    20180730-宿主机开发环境搭建
    20180319-双网卡电脑同时上内外网
    嵌入式ARM板子起步
    20180127-服务器开发环境搭建
    Pool多进程示例
    Python基础-day01
    解释型语言与编译型语言
    C 编译过程浅析
    博客奇谭
  • 原文地址:https://www.cnblogs.com/steamedbun/p/5693384.html
Copyright © 2011-2022 走看看