(hdu1394)Minimum Inversion Number (树状数组/线段树/归并排序)

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

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

题意:
给定一个长度为n的序列,问每次可以将序列的第一个数移到最后一个,它的逆序数最小是多少?

分析:首先可以分析得知把序列的首元素移到最后一个,其逆序数的变化为n-a[i]-1(增加的),a[i](减少的);假设未移动时的逆序数个数为sum,移动后的逆序数个数就等于sum+n-a[i]-1-a[i].

此题可以用线段树,后者树状数组求解,当然也可以用归并排序,在这儿不针对归并排序分析,此题的代码可以用树状数组写,而且不容易出错,能用树状数组的尽量用它写。

1.线段树

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=5e3+5;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int sum[N<<2],a[N];///sum开4倍空间较为安全
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);
    if(p>mid) update(p,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return sum[rt];
    }
    int res=0;
    int mid=(l+r)>>1;
    if(L<=mid) res+=query(L,R,lson);
    if(R>mid) res+=query(L,R,rson);
    return res;
}
int main()
{
    int n,sum=0;
    while(~scanf("%d",&n))
    {
        sum=0;
        build(1,n,1);///建立线段树
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum+=query(a[i]+1,n,1,n,1);//对当前输入的数计算当前序列的逆序数
            update(a[i],1,n,1);
        }
        int ans=sum;///此时sum为输入序列的总的逆序数
        for(int i=0;i<n;i++)
        {
            sum+=n-a[i]-1-a[i];///如分析
            ans=min(ans,sum);///求最小的
        }
        printf("%d\n",ans);
    }
    return 0;
}

这里写图片描述

2.树状数组

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5005;
int c[N],a[N],n;
int lowbit(int i) {
    return i&(-i);
}
void update(int pos,int val) {
    while(pos<=n) {
        c[pos]+=val;
        pos+=lowbit(pos);
    }
}
int sum(int i) {
    int s=0;
    while(i>0) {
        s+=c[i];
        i-=lowbit(i);
    }
    return s;
}
int main() {
    while(~scanf("%d",&n)) {
        int res=0;
        memset(c,0,sizeof(c));
        for(int i=0; i<n; i++) {
            scanf("%d",&a[i]);
            a[i]++;
            res+=sum(n)-sum(a[i]);///当前序列的逆序数对的个数
           // printf("%-3d %-3d %-3d %-3d  ",a[i],sum(n),sum(a[i]),res);
            update(a[i],1);///修改BIT中Aj位置上的值
            /*printf("%-3d\n",c[i]);
            puts("-------------");
            for(int i=1;i<=n;i++) printf("%d ",c[i]);
            puts("");
            puts("-------------");*/
        }
        int Min=res;
        for(int i=0; i<n; i++) {
            res+=n-a[i]-(a[i]-1); ///移动后的逆序数与移动前的逆序数对的关系,减去比之前小的,加上比之后大的
           // printf("res=%d\n",res);
            Min=min(Min,res);///取最小的逆序数对的个数
        }
        printf("%d\n",Min);
    }
    return 0;
}

这里写图片描述

明显这里用树状数组比较简洁方便,不容易出错。

3.归并排序

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=10056;
const int INF=0x3f3f3f3f;
const int N=5e3+5;
typedef long long LL;
#define mem(a,n) memset(a,n,sizeof(a))
int a[N],b[N],T[N];
int cnt=0;
void merge_sort(int *A,int x,int y)
{
    if(y-x>1)
    {
        int m=(x+y)/2;
        int l=x,i=x,r=m;
        merge_sort(A,x,m);
        merge_sort(A,m,y);
        while(l<m||r<y)///两个序列只要有一个非空
        {
            if(r>=y||(l<m&&A[l]<=A[r]))//r>=y表示第二个序列为空时,后面的表示当两个都非空时,左边序列的元素小于右边的,满足这两个条件才复制左边的
                T[i++]=A[l++];
            else否则复制右边的
            {
                T[i++]=A[r++];
                cnt+=(m-l);
            }
        }
        for(int i=x; i<y; i++)///把辅助数组中的元素复制回数组A
            A[i]=T[i];
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        cnt=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            b[i]=a[i];
        }
        merge_sort(a,0,n);
        int MIN=cnt;
        for(int i=0; i<n; i++)
        {
            cnt+=(n-1-b[i]-b[i]);
            MIN=min(MIN,cnt);
        }
        printf("%d\n",MIN);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值