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): 20570    Accepted Submission(s): 12335


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
 

题意:
重复一次操作可以把最前面的树放到最后面。求最小的逆序总数,逆序数是i<j且Ai>Bi。

POINT:
因为给你N后数是确定的,是0-(n-1),所以只要知道最前面的数的值,就可以知道把它移到后面后逆序总数的变化。
比第一个小的数量必为Ai, 大的必为n-Ai-1。这是这题的关键。
下面则是要先算出初始状态时的逆序总数,再根据它一次一次移动求出每个的逆序总数,在和ans比较。

这里有两种方法求初始状态时的逆序总数。
法1:暴力,每输入一个数,去找前面有几个比他大的,记录答案。显然这个非常简单。
法2:利用线段树,此时的线段树的区间直接代表数值,存的则是这些数出现了几次。
即num[x]代表值在l和r的数出现了几次。也是每输入一个数,存下这个数,更新区间,再去询问一次Ai+1到n+1的数出现了几次。

法1:显然比线段树短很多,但是也慢很多。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
const int N = 5000+5;
int a[N];
int sum[N];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(sum,0,sizeof sum);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            for(int j=1;j<i;j++)
            {
                if(a[j]>a[i])
                {
                    sum[i]++;
                }
            }
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=sum[i];
        }
        int pre=ans;
        for(int i=1;i<=n;i++)
        {
            int now=pre;
            int xiao=a[i];
            int da=n-a[i]-1;//不包括自己
            pre=now+da-xiao;
            ans=min(ans,pre);
        }
        printf("%d\n",ans);
    }

    

法2:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
const int N = 5000*4;
int a[N/4];
int num[N];
void add(int x,int l,int r,int now)
{
    if(l==r&&r==now) num[x]++;
    else
    {
        int mid=(l+r)>>1;
        if(now<=mid) add(x*2,l,mid,now);
        if(mid<now) add(2*x+1,mid+1,r,now);
        num[x]=num[2*x+1]+num[x*2];
    }
}
int query(int x,int l,int r,int ll,int rr)
{
    int ans=0;
    if(ll<=l&&rr>=r) ans=num[x];
    else
    {
        int mid=(l+r)>>1;
        if(ll<=mid) ans+=query(x*2,l,mid,ll,rr);
        if(mid<rr) ans+=query(2*x+1,mid+1,r,ll,rr);
    }
    return ans;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(num,0,sizeof num);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            add(1,0,n-1,a[i]);
            ans+=query(1,0,n-1,a[i]+1,n-1);
        }
        int pre=ans;
        for(int i=1;i<=n;i++)
        {
            int now=pre;
            now=pre-a[i]+n-a[i]-1;
            ans=min(now,ans);
            pre=now;
        }
        printf("%d\n",ans);
    }
    
    
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值