hdu:Minimum Inversion Number

Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23024 Accepted Submission(s): 13691

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
ZOJ Monthly, January 2003

Recommend
Ignatius.L

题意:
给出由0到n-1组成的序列,可以操作序列让序列的开头放到序列的末尾,求 通过操作形成的序列 的逆序对数的最小值。

一个数字 a[i] 在开头,后面比它小的数字有 a[i] 个(序列又 0-n-1 构成),也就是说和这个数字组成逆序的有 a[i] 个,把它放到最后,原来比他小的数字就不和他构成逆序了,而比他大的数字就和他构成了逆序;所以每次移动 逆序数的增量是 n - 1 - a[i] - a[i] 。

逆序对数是说序列的每个数 a[i] 在i之后比 a[i] 小的个数 和;也就是说序列的每个数 a[i] 在 i 之前出现比 a[i] 大的个数和,即输入 a[i] 时统计之前出现过的 比 a[i] 大的个数(即和 a[i] 构成逆序对的个数),求和,即是序列的总逆序数。

在输入时统计之前出现过的 比 a[i] 大的个数,可以用线段树完成;即线段树维护的是区间内数字出现的个数,而我们要求的逆序对个数就可以通过 查询区间 ( a[i],n-1 ) 的值 得到;

构造一棵空树,即初始任何区间的值都为零;然后统计总逆序对数,再利用得到的增量遍历一遍求得逆序数的最小值。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 5010;
int sum[maxn << 2], a[maxn];
int x, v;
void update(int pos, int l, int r) {
    if (l == r) {
        sum[pos] += v;
        return ;
    }
    int mid = (l + r) >> 1;
    if (x <= mid)
        update(pos << 1, l, mid);
    else
        update(pos << 1 | 1, mid + 1, r);
    sum[pos] = sum[pos << 1] + sum[pos << 1 | 1];
    return ;
}
int ql, qr;
int query(int pos, int l, int r) {
    int ans = 0;
    if (ql <= l && r <= qr)
        return sum[pos];
    int mid = (l + r) >> 1;
    if (ql <= mid)
        ans += query(pos << 1, l, mid);
    if (qr > mid)
        ans += query(pos << 1 | 1, mid + 1, r);
    return ans;
}
int main() {
    int n;
    while (~scanf("%d", &n)) {
        memset(sum, 0, sizeof(sum));
        int ans = 0;
        qr = n - 1;
        v = 1;
        for (int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
            ql = a[i];
            x = a[i];
            ans += query(1, 0, n);
            update(1, 0, n);
        }
        int cnt = ans;
        //cout << ans << endl;
        for (int i = 0; i < (n - 1); i++) {
            cnt += (n - 1 - a[i] - a[i]);
            ans = min(ans, cnt);
        }
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值