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): 16585 Accepted Submission(s): 10093

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

题目意思在这了就不多做解释了。

有两种方法求逆序对:

第一种,暴力枚举。因为这n个数是从0到n-1。先暴力求出原本的序列的逆序对,然后每移一个数字到最后面的话,用x表示现在序列的逆序对数,用y表示下一个序列的逆序对用ai表示移动的数字,那么可以得到以下的关系。原先序列的逆序对减少了ai个,变出来的序列增加(n-ai-1)个逆序对,(这个规律只对从0到n-1的连续数字使用,对别的逆序对问题不合适),y=x-ai-ai+n-1。

下面是AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int a[5005];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int ans=99999999;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        int sum=0;
        for(int i=0; i<n; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(a[i]>a[j])
                {
                    sum++;
                }
            }
        }
        if(ans>sum)
        {
            ans=sum;
        }
        for(int i=0; i<n; i++)
        {
            sum=sum-a[i]-a[i]-1+n;
            if(ans>sum)
            {
                ans=sum;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

下面是线段树的AC代码:

先写上有时间再来解释。

#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=5550;
struct node
{
    int left,right,val;
} c[maxn*3];

void Pushup(int root)
{
    c[root].val=c[root*2].val+c[root*2+1].val;
}

void build_tree(int l,int r,int root)
{
    c[root].left=l;
    c[root].right=r;
    if(l==r)
    {
        return ;
    }
    int mid=(l+r)/2;
    build_tree(l,mid,root*2);
    build_tree(mid+1,r,root*2+1);
}

void update_tree(int pos,int root)
{
    if(c[root].left==c[root].right)
    {
        c[root].val++;
        return ;
    }
    int mid=(c[root].left+c[root].right)/2;
    if(pos<=mid)
    {
        update_tree(pos,root*2);
    }
    else
    {
        update_tree(pos,root*2+1);
    }
    Pushup(root);
}

int search_tree(int L,int R,int root)
{
    if(L==c[root].left&&R==c[root].right)
    {
        return c[root].val;
    }
    int ret=0;
    int mid=(c[root].left+c[root].right)/2;
    if(R<=mid)
    {
        return search_tree(L,R,root*2);
    }
    else if(L>mid)
    {
        return search_tree(L,R,root*2+1);
    }
    else
    {
        return search_tree(L,mid,root*2)+search_tree(mid+1,R,root*2+1);
    }
}

int x[maxn];

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(c,0,sizeof(c));
        build_tree(0,n-1,1);
        int sum=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&x[i]);
            sum+=search_tree(x[i],n-1,1);
            update_tree(x[i],1);
        }
        int ret=sum;
        for(int i=0; i<n; i++)
        {
            sum+=n-x[i]-x[i]-1;
            ret=min(ret,sum);
        }
        printf("%d\n",ret);
    }
    return 0;
}
时间: 2024-08-26 23:56:39

HDU 1394 Minimum Inversion Number(逆序对问题)的相关文章

hdu 1394 Minimum Inversion Number 逆序数/树状数组

Minimum Inversion Number Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1394 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

HDU 1394 &lt;Minimum Inversion Number&gt; &lt;逆序数&gt;&lt;线段树&gt;

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 seq

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

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1394 解题报告:给出一个序列,求出这个序列的逆序数,然后依次将第一个数移动到最后一位,求在这个过程中,逆序数最小的序列的逆序数是多少? 这题有一个好处是输入的序列保证是0 到 n-1,所以不许要离散化,还有一个好处就是在计算在这个序列中比每个数大和小的数一共有多少个的时候可以在O(1)时间计算出来,一开始我没有意识到,还傻傻的用了两层for循环来每次都计算,当然这样果断TLE了.把一个数从第一个移

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列: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)求这些序列中,逆序数最少的

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

题目地址:HDU 1394 这题可以用线段树来求逆序数. 这题的维护信息为每个数是否已经出现.每次输入后,都从该点的值到n-1进行查询,每次发现出现了一个数,由于是从该数的后面开始找的,这个数肯定是比该数大的.那就是一对逆序数,然后逆序数+1.最后求完所有的逆序数之后,剩下的就可以递推出来了.因为假如目前的第一个数是x,那当把他放到最后面的时候,少的逆序数是本来后面比他小的数的个数.多的逆序数就是放到后面后前面比他大的数的个数.因为所有数都是从0到n-1.所以比他小的数就是x,比他大的数就是n-

HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由[1,N]构成,可以通过旋转把第一个移动到最后一个. 问旋转后最小的逆序数对. 分析: 注意,序列是由[1,N]构成的,我们模拟下旋转,总的逆序数对会有规律的变化. 求出初始的逆序数对再循环一遍就行了. 至于求逆序数对,我以前用归并排序解过这道题:点这里. 不过由于数据范围是5000,所以完全可以用线

HDU 1394——Minimum Inversion Number(最小逆序数)

题意: 给定一个序列,里面的数是0到n-1,  每次要把第一个数放到最后一个数,重复n次,求n次操作中最小的逆序数是多少? 思路: 先求出初始的逆序数,然后每移动第一个数到最后面,那么逆序数要减去比它小的数的个数,加上比它大的数的个数. 如果我输入的数是a[i],那么比它小的数的个数就有a[i]个,比它大的数的个数就有n-1-a[i]个 方法:    归并排序 ,树状数组,线段树 归并排序代码: #include<iostream> #include<cstring> #inclu

HDU 1394 Minimum Inversion Number(逆序数)

题目链接:HDU 1394 Minimum Inversion Number [题意]给你一个1~N的数字组成的初始序列,然后每一次都将第一个数字移到最后,形成新的序列,然后求出这些序列的逆序数中的最小值. [思路]开始可以用任意一种方法(线段树 or 暴力 or 树状数组)计算出初始数列的逆序数sum,这里我比较懒,就直接用的暴力找的sum,对于a[i](0~~n-1),每挪一个,用sum减去挪之前它右边比它小的数的个数(也就是a[i]个),再用sum加上挪之后左边比它大的数的个数(也就是n-

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

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 seq

hdu 1394 Minimum Inversion Number

题目链接:hdu 1394 Minimum Inversion Number 该题是求最小逆序对的扩展.可以使用树状数组来实现.对于$n$个数的序列$A$,其第$i$个数($i\in [0,n)$)的逆序数$r_i$可以表示为它的角标$i$减去在它之前且不大于它的数的个数.例如对序列A = {1,3,5,9,0,8,5,7,4,2}中的数,A[8] = 4.其逆序数$r_8 = 8 - 3 = 5$,第二个3表示三个在它前面且比它小的数:{1,3,0}.从而我们可以得到第$i$个数的逆序数公式: