您的位置:首页 > 其它

hdoj 1394 Minimum Inversion Number 线段树||树状数组||分治

2017-05-15 23:40 357 查看
题目:


Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 19983    Accepted Submission(s): 11998


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

这个题求逆序数,唔,我的解法是线段树,先挖个坑,等我学习一波树状数组和分治后再回来学习一个。

这个题的条件给的是比较弱的,0——N-1的一个排列,求有多少逆序数对,这个为我们提供了方便。

思路:

求逆序数:

按顺序从左到右插入,先询问元素x到N-1之间这个区间有多少个数x,ret+=x,再把每个元素放置在它应该在的位置,以1,0,2,4,3为例,这里N=5,首先第一个数是1,询问[1,4]这个区间上已经有了多少个数a,那说明这a个数在1之前已经插入了,也就是说这a个数比1大,但位置在1之前,因此ret+=a.

(0)ret=0

(1)询问[1,4],ret+=0,插入1.

(2)询问[0,4],ret+=1,(已经插入了1),再插入0

(3)询问[2,4],ret+=0,再插入2

(4)询问[4,4],ret+=0,再插入4

(5)询问[3,4],ret+=1,(在这之前已经插入了4),再插入3

(6)结束

2,维护,暨每次把队头放在队尾。我们可以推出公式:

逆序数减少:a[i]

逆序数增加:N-1-a[i]

sum+=N-1-a[i]-a[i]

ret=min(ret,sum)

code:

#include<cstdio>

#include<cstring>

#define lson l,m,rt<<1

#define rson m+1,r,rt<<1|1

#define min(a,b) (a<b?a:b)

const int MAXN=5000+5;

int a[MAXN<<2];

void PushUp(int rt){

    a[rt]=a[rt<<1]+a[rt<<1|1];

}

void update(int p,int l,int r,int rt){

    if(l==r){

        a[rt]++;

        return;

    }

    int m=(l+r)>>1;

    if(p<=m)update(p,lson);

    else update(p,rson);

    PushUp(rt);

}

int Query(int L,int R,int l,int r,int rt){

    if(l>=L&&r<=R)return a[rt];

    int m=(l+r)>>1;

    int ret=0;

    if(L<=m)ret+=Query(L,R,lson);

    if(R>m)ret+=Query(L,R,rson);

    return ret;

}

int x[MAXN];

int main(void){

    int N;

    while(scanf("%d",&N)==1){

        memset(a,0,sizeof(a));

        int sum=0;

        for(int i=0;i<N;++i){

            scanf("%d",&x[i]);

            sum+=Query(x[i],N-1,0,N-1,1);

            update(x[i],0,N-1,1);

        }

        int ret=sum;

        for(int i=0;i<N;++i){

            //printf("ret=%d\n",ret);

            sum+=N-x[i]-1-x[i];

            ret=min(sum,ret);

        }

        printf("%d\n",ret);

    }

    return 0;

}

关于求逆序数:

然后我们考虑,如果它不是一个排列,而是任意N个无重复整数,N<=1e5,a[i]<=1e9,那么我们该怎么做?bingo,离散化,先sort后再重新标序号,如 3,13456789,123456789,我们让map[3]=0,map[13456789]=1,map[123456789]=2,然后又是刚才的解法。

然后,我们再想如果有重复我们该怎么办,逆序数对定义,i<j&&a[i]>a[j],因此在原代码的改进上,我们只需要访问[a+1,N-1]之间的就好了,另比如第某个数是1,我们就看[2,N-1]中已经有多少个数了。注意排序后标序号时就ok了。

附上ugly代码:

#include<cstdio>

#include<map>

#include<algorithm>

#include<cstring>

#define lson l,m,rt<<1

#define rson m+1,r,rt<<1|1

#define min(a,b) (a<b?a:b)

using namespace std;

const int MAXN=5000+5;

int a[MAXN<<2];

void PushUp(int rt){

    a[rt]=a[rt<<1]+a[rt<<1|1];

}

void update(int p,int l,int r,int rt){

    if(l==r){

        a[rt]++;

        return;

    }

    int m=(l+r)>>1;

    if(p<=m)update(p,lson);

    else update(p,rson);

    PushUp(rt);

}

int Query(int L,int R,int l,int r,int rt){

    if(l>=L&&r<=R)return a[rt];

    int m=(l+r)>>1;

    int ret=0;

    if(L<=m)ret+=Query(L,R,lson);

    if(R>m)ret+=Query(L,R,rson);

    return ret;

}

int x[MAXN],temp[MAXN];

int main(void){

    int N;

    while(scanf("%d",&N)==1){

        map<int,int>m;

        for(int i=0;i<N;++i){

            scanf("%d",temp+i);

            x[i]=temp[i];

        }

        sort(temp,temp+N);

        int index=0;m[temp[0]]=index;

        for(int i=1;i<N;++i)

            if(temp[i]!=temp[i-1])m[temp[i]]=++index;

            else m[temp[i]]=index;

        memset(a,0,sizeof(a));

        int sum=0;

        for(int i=0;i<N;++i){

 
b10f
          x[i]=m[x[i]];

            if(x[i]+1<=N-1)sum+=Query(x[i]+1,N-1,0,N-1,1);

            /*其实应该是这样,但我们发现else 没意义

            if(x[i]+1<=N-1)

                sum+=Query(x[i]+1,N-1,0,N-1,1);

            else

                sum+=Query(N-1,N-1,0,N-1,1)*/

            update(x[i],0,N-1,1);

        }

        printf("ret=%d\n",sum);

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息