hdu Minimum Inversion Number(线段树求逆序数有关问题的一个小归纳)

来源:互联网 发布:软件总体技术方案 编辑:程序博客网 时间:2024/04/26 17:09

Minimum Inversion Number

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


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
101 3 6 9 0 8 5 7 4 2
 

Sample Output
16
 

Author
CHEN, Gaoli
 

Source
ZOJ Monthly, January 2003
 

Recommend
Ignatius.L


首先,求逆序数对的思路:
1.得到整个数列后,从前往后扫,统计比a[i]小的,在a[i]后面的有多少个
这样做的话,应该是只有n2的暴力作法,没想到更好的方法
2.统计a[i]前面的,且比它大的数
这样做的话,就可以利用输入的时效性,每输入一个数,就把这个数的num[i]值加1,
然后统计比这个数大的数的num和,
因为这里的和一定是在这个数列中比a[i]大,且在它前面出现的数之和,
然后把把这个和加到总逆序数sum里。
这样做的话直接的暴力作法依然是n2,但是,
我们可以在,统计比这个数大的数的num和这一步进行优化,利用线段树求区间域值的复杂度是logn,
所以总体复杂度就降到了nlogn。

再来看这道题,求得初始数列的逆序数后,再求其他排列的逆序数有一个规律,就是
sum = sum + (n - 1 - a[i]) - a[i];
这个自行验证吧,相信很容易得出

最后,拓展一下,如果要求正序数怎么办?很简单,无非是大小调一下
再问,如果要求满足i<j<k,且a[i]>a[j]>a[k]的数对总数怎么办?

可以从中间的这个数入手,统计a[i]>a[j]的对数m,以及a[j]>a[k]的对数n,m*n就是。。。
要求a[i]>a[j]的个数还是一样的,那么a[j]>a[k]的个数呢?
两种思路:
1.得到a[i]>a[j]的对数后,将数列倒过来后再求a[j]<a[k]的对数
2.更简单的做法是,找到规律发现,n = 整个数列中比a[j]小的数 — 在a[j]前面已经出现的比a[j]小的数的个数
即(假设数列是从1开始的) n = (a[j] -1) - (j - 1 - m )



这道题代码
#include <stdio.h>#include <algorithm>using namespace std;int a[5005];struct Node{    int l,r,num;}tree[50000];void Build(int n,int x,int y){    tree[n].l = x;    tree[n].r = y;    tree[n].num = 0;    if(x == y){        return;    }    int mid = (x + y) / 2;    Build(2*n,x,mid);    Build(2*n+1,mid+1,y);}void Modify(int n,int x){    int l = tree[n].l;    int r = tree[n].r;    int mid = (l + r) / 2;    if(x == l && x == r){        tree[n].num = 1;        return;    }    if(x <= mid)    Modify(2*n,x);    else            Modify(2*n+1,x);    tree[n].num = tree[2*n].num + tree[2*n+1].num;}int Query(int n,int x,int y){    int l = tree[n].l;    int r = tree[n].r;    int mid = (l + r) / 2;    int ans = 0;;    if(x == l && y == r)        return tree[n].num;    if(x <= mid)   ans += Query(2*n,x,min(mid,y));    if(y > mid)    ans += Query(2*n+1,max(mid+1,x),y);    return ans;}int main(){    int n,sum,ans;    int i,j;    while(scanf("%d",&n) != EOF){        sum = 0;        Build(1,0,n);        for(i = 1;i <= n;i++){            scanf("%d",&a[i]);            Modify(1,a[i]);            sum += Query(1,a[i]+1,n);        }        ans = sum;        for(i = 1;i < n;i++){            sum = sum + (n - 1 - a[i]) - a[i];            if(sum < ans)                ans = sum;        }        printf("%d\n",ans);    }}



原创粉丝点击