hdu1394 Minimum Inversion Number(线段树)

Minimum Inversion Number

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


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 

题意:给定一个序列,每次把第一个元素放到最后,求最小的逆序数。
分析:处理一串序列还好处理,可是序列一变就不知道怎么处理了,想了很久,未果,参考了大牛博客,才知道这个事有规律的, 如果是0到n的排列,那么如果把第一个数放到最后,对于这个数列,逆序数是减少a[i],而增加n-1-a[i]的。然后就解决了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 50010

struct node
{
    int l,r,s;
}t[4*MAXN];
int n,ans;

void build(int x, int y, int num)
{
    t[num].l = x;
    t[num].r = y;
    t[num].s = 0;
    if(x == y) return ;
    int mid = (x+y)/2;
    build(x, mid, num*2);
    build(mid+1, y, num*2+1);
}

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

int query(int x, int num)
{
    if(t[num].l >= x && t[num].r < n) return t[num].s;
    else
    {
        int sum1=0,sum2=0;
        int mid = (t[num].l+t[num].r)/2;
        if(x <= mid) sum1 = query(x, num*2);
        if(n-1 > mid) sum2 = query(x, num*2+1);
        return sum1+sum2;
    }
}

int main()
{
    while(scanf("%d",&n)==1)
    {
        build(0, n-1, 1);
        int a[MAXN];
        ans = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            ans += query(a[i]+1, 1);
            update(a[i], 1);
        }
        int minx = ans;
        for(int i=0; i<n; i++)
        {
            ans = ans+n-2*a[i]-1;
            if(ans < minx) minx = ans;
        }
        printf("%d\n",minx);
    }
    return 0;
}


你可能感兴趣的:(线段树)