HDU 1394 Minimum Inversion Number(线段树)

Minimum Inversion Number

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


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且不一定有序排列的数字串,不断的将第一个数字放到数字串的最后,可以得到n个不同的数字串。每一个数字串都有一个逆序数,求n个数字串中最小的逆序数。

思路:我们可以先确定第一个数字串的逆序数,之后每一次将首个数字移到数字串的尾部都有一个规律,那就是逆序数减去a[i],再加上(n-a[i]-1),a[i]代表第i个数字。所以我们可以先建一个空树,然后依照第一个数字串依次往线段树中填充数字,每填充一个数字都找出在它之前比它大的数字有多少个。最后求出第一个数字串的逆序数之后根据已经找出来的规律递推一下得到最小的逆序数。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=5005;
struct node
{
    int l,r;
    int num;//本节点表示区间内数字的个数
}segTree[4*MAXN];
int a[MAXN];

void build(int root,int l,int r)
{
    segTree[root].l=l;
    segTree[root].r=r;
    segTree[root].num=0;
    if(segTree[root].l==segTree[root].r)
        return;
    int mid=(l+r)>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
}

void update(int root,int x)
{
    if(segTree[root].l==segTree[root].r)
    {
        if(segTree[root].l==x)
            segTree[root].num++;
            return;
    }
    int mid=(segTree[root].l+segTree[root].r)>>1;
    if(x<=mid)
        update(root<<1,x);
    else
        update(root<<1|1,x);
    //回溯更新
    segTree[root].num=segTree[root<<1].num+segTree[root<<1|1].num;

}

int query(int root,int l,int r)
{
    if(l>r)//因为数字的大小是0到n-1,可能会出现l>r的情况
        return 0;
    if(segTree[root].l==l&&segTree[root].r==r)
        return segTree[root].num;
    int mid=(segTree[root].l+segTree[root].r)>>1;
    if(r<=mid)
        return query(root<<1,l,r);
    else if(l>mid)
        return query(root<<1|1,l,r);
    else
        return query(root<<1,l,mid)+query(root<<1|1,mid+1,r);
}

int main()
{
    int n,i,sum,ans;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        build(1,0,n-1);
        sum=0;
        for(i=0;i<n;i++)
        {
            //找出在这个数字之前比它大的有多少个,并且累加
            sum+=query(1,a[i]+1,n-1);
            //更新节点
            update(1,a[i]);
        }
        ans=sum;
        //找出最小的逆序数
        for(i=0;i<n-1;i++)
        {
            sum=sum-a[i]+n-a[i]-1;
            if(sum<ans)
                ans=sum;
        }
        printf("%d\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值