HDU 1394- Minimum Inversion Number(线段树求逆序数)

来源:互联网 发布:java加密解密的艺术 编辑:程序博客网 时间:2024/05/07 10:02

Minimum Inversion Number
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1394
Appoint description: 

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
 

题意:求序列的逆序数,每求完一次将该序列的第一个数放在最后一个,然后再求逆序数,一直重复,然后求最小的逆序数。

思路:用线段树记录下各个数,区间的值[a,b]表示数字a~b的已经出现了多少次,所以对于ai,只需要查询[ai, n]有多少个(ai之后的比ai小的有多少个),就是代表ai有多少个逆序数了。然后通过推理我们可以发现:假如目前的第一个数是a[i],那当把他放到最后面的时候,少的逆序数是本来后面比他小的数的个数。多的逆序数就是放到后面后前面比他大的数的个数。因为所有数都是从0到n-1.所以比他小的数就是a[i],比他大的数就是n-1-a[i]。所以算出初始序列的逆序数,然后用出事序列的逆序数+n-2*a[i]-1就是改变后的序列的逆序数。


#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1const int MAXN=5010;int sum[MAXN<<2];void PushUp(int rt){    sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void Build(int l,int r,int rt){    sum[rt]=0;    if(l==r) {        return ;    }    int mid=(l+r)>>1;    Build(lson);    Build(rson);    PushUp(rt);}void Update(int p,int l,int r,int rt){    if(l==r) {        sum[rt]++;        return ;    }    int mid=(l+r)>>1;    if(p<=mid)        Update(p,lson);    else        Update(p,rson);    PushUp(rt);}int Query(int ll,int rr,int l,int r,int rt){    if(ll<=l&&rr>=r) {        return sum[rt];    }    int mid=(l+r)/2;    int ans=0;    if(ll<=mid)        ans+=Query(ll,rr,lson);    if(rr>mid)        ans+=Query(ll,rr,rson);    return ans;}int main(){    int n,i;    int a[MAXN];    int ans;    int x;    while(~scanf("%d",&n)) {        ans=0;        Build(0,n-1,1);        for(i=0; i<n; i++) {            scanf("%d",&a[i]);            ans+=Query(a[i],n-1,0,n-1,1);            Update(a[i],0,n-1,1);        }        x=ans;        for(i=0; i<n; i++) {            x+=n-2*a[i]-1;            ans=min(ans,x);        }        printf("%d\n",ans);    }    return 0;}


1 0
原创粉丝点击