HDU1394 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


题目大意:
n个数,从0~n-1
求出一个排列,他的逆序对数最小

思路:
求出一个组合的逆序对数后,如3 4 1 2 0 的逆序对数为8
把3放在最后,变成4 1 2 0 3,设它的逆序对数为ans。
因为3放在最后会少了3 1 、3 2、 3 0 这3(a[i])个逆序对
同时3放在最后又会多了4 3这1(n-a[i]-1)个逆序对
所以ans = 8 - a[i] + n - a[i] - 1 = 8+n-2*a[i]-1
求一个最小的ans即可

求逆序对的个数常用 归并排序、树状数组(树状数组求逆序对 ),线段树也可以。

要点:
数据范围从0~n-1,但是线段树区间下标必须从1开始
在线建树、查询

线段树

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

const int maxn = 50010;
struct SegmentTree
{
    int l, r;
    int sum;
}tree[maxn<<2];
int a[maxn];
int n;

void pushup(int p)
{
    tree[p].sum = tree[p<<1].sum + tree[p<<1|1].sum;
}

void build(int p, int l, int r)
{
    tree[p].l = l;
    tree[p].r = r;
    if (l == r)
    {
        tree[p].sum = 0;
        return;
    }
    int mid = (l + r) >> 1;
    build(p<<1, l, mid);
    build(p<<1|1, mid+1, r);
}

void change(int p, int x)
{
    if (tree[p].l == tree[p].r)
    {
        tree[p].sum ++;
        return;
    }
    int mid = (tree[p].l + tree[p].r) >> 1;
    if (x <= mid) change(p<<1, x);
    else change(p<<1|1, x);
    pushup(p);
}

int ask(int p, int l, int r)
{
    if (l <= tree[p].l && r >= tree[p].r) 
        return tree[p].sum;
    int mid = (tree[p].l + tree[p].r) >> 1;
    int ans = 0;
    if (l <= mid)
        ans += ask(p<<1, l, r);
    if (r > mid) 
        ans += ask(p<<1|1, l, r);
    return ans;
}

int main()
{
    while (cin >> n)
    {
        memset(tree, 0, sizeof tree);
        memset(a, 0, sizeof a);
        build(1, 0, n-1);
        int ans = 0;
        for (int i = 0; i < n; i ++)
        {
            cin >> a[i];
            ans += ask(1, a[i], n-1);
            change(1, a[i]);
        }
        int ret = ans;
        for (int i = 0; i < n; i++)
        {
            ans += n - 2 * a[i] - 1;
            ret = min(ret, ans);
        }
        cout << ret << endl;
    }
    return 0;
}

树状数组

#include <algorithm>
#include <cstdio>
#include <iostream>
#define M 5010
using namespace std;
using ll = long long;
int a[M], d[M], c[M], n;

// a[]为原数组
// d[k]表示第k大的数在a数组中的位置

void add(int x)
{
    for (; x <= n; x += x & -x)
        c[x]++;
}

int ask(int x)
{
    int ans = 0;
    for (; x; x -= x & -x)
        ans += c[x];
    return ans;
}

bool cmp(int x, int y)
{
    if (a[x] == a[y])
        return x > y;
    return a[x] > a[y];
}

int main()
{
    while (cin >> n)
    {
        memset(a, 0, sizeof a);
        memset(d, 0, sizeof d);
        memset(c, 0, sizeof c);
        int ans = 0;
        for (int i = 1; i <= n; i++)
            cin >> a[i], d[i] = i;

        sort(d + 1, d + n + 1, cmp);

        for (int i = 1; i <= n; i++)
        {
            add(d[i]);            // 相当于a[]中第i个数出现了一次
            ans += ask(d[i] - 1); // 相当于求a[]中第i个数之前比他大的数的个数
        }

        int ret = ans;
        for (int i = 1; i <= n; i++)
        {
            ans += n - 2 * a[i] - 1;
            ret = min(ret, ans);
        }
        cout << ret << endl;
    }
    
    return 0;
}

归并排序

#include <cstdio>
#include <iostream>
using namespace std;
using ll = long long;
const int maxn = 5010;
int a[maxn], b[maxn], k[maxn];
int sum;
void Merge(int l, int mid, int r)
{
    int p = 0;
    int i = l, j = mid + 1;
    while (i <= mid && j <= r)
    {
        if (a[i] > a[j])
        {
            sum += mid - i + 1;
            k[p++] = a[j++];
        }
        else
            k[p++] = a[i++];
    }
    while (i <= mid)
        k[p++] = a[i++];
    while (j <= r)
        k[p++] = a[j++];
    for (i = 0; i < p; i++)
        a[l + i] = k[i];
}
void MergeSort(int l, int r)
{
    if (l < r)
    {
        int mid = (l + r) >> 1;
        MergeSort(l, mid);
        MergeSort(mid + 1, r);
        Merge(l, mid, r);
    }
}
int main()
{
    int n;
    while (cin >> n)
    {
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            b[i] = a[i];
        }
        sum = 0;
        MergeSort(0, n - 1);
        int ans = sum;
        for (int i = 0; i < n - 1; i++)
        {
            sum += n - b[i] - b[i] - 1;
            ans = ans < sum ? ans : sum;
        }
        cout << ans << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值