hdu1394 [Minimum Inversion Number]

Minimum Inversion Number

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.

InputThe 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. 
OutputFor 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

题目大意:  ai是0——n-1之间的一个数字,且互不重复;  给你一个长度为n的数列{ai},求以其中某个数为起点的循环队列中的逆序对个数最小;  For example:    {a1,a2,a3..,an}    {a2,a3..,an,a1}    {a3,..an,a1,a2}    {an,a1,a2,..an-1}

Tips:    因为ai是0——n-1之间的一个数字,且互不重复;  所以我们可以知道当ai为开头时,有多少数比它大,有多少数比它小;  F[i]表示以第i个数为起点的逆序对个数;  所以先用(线段树|树状数组|归并排序)求出原序列的逆序对数设为F[1];  所以不难推出F[i]=F[i-1]-a[i-1](有a[i-1]个比它小的数)+n-a[i-1]-1(有n-a[i-1]-1个比它大的数),即F[i]=F[i-1]+n-2*a[i-1]-1;  最后取最小值,输出即可;

Code:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define MAXN 200008
using namespace std;
int ans,n,m,a[MAXN],tree[MAXN],sum;

void add(int l,int r,int x,int v){
    if(l==r){
        tree[v]++;
        return;
    }
    int mid=(l+r) >> 1;
    if(x<=mid)
        add(l,mid,x,v<<1);
    else
        add(mid+1,r,x,(v<<1)+1);
    tree[v]=tree[v<<1]+tree[(v<<1)+1];
}

int query(int l,int r,int x,int y,int v){
    if(l==x&&r==y){
        return tree[v];
    }
    int mid=(l+r) >> 1;
    if(y<=mid)
        return query(l,mid,x,y,v<<1);
    if(x>mid)
        return query(mid+1,r,x,y,(v<<1)+1);
    return query(l,mid,x,mid,v<<1)+query(mid+1,r,mid+1,y,(v<<1)+1);
}

int    main(){
    while(scanf("%d",&n)!=EOF){
        memset(tree,0,sizeof(tree));
        sum=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            a[i]++;
            sum+=query(1,n,a[i],n,1);
            add(1,n,a[i],1);
        }
        ans=sum;
        for(int i=1;i<=n-1;i++){
            sum=sum-a[i]+1+n-a[i];
            ans=min(ans,sum);
        }
        printf("%d\n",ans);
    }
}
时间: 2024-10-12 00:39:49

hdu1394 [Minimum Inversion Number]的相关文章

HDU1394 Minimum Inversion Number 【线段树】+【逆序数】

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9864    Accepted Submission(s): 6069 Problem Description The inversion number of a given number sequence a1, a2, ..., an

HDU1394 Minimum Inversion Number 线段树+数学

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

[hdu1394]Minimum Inversion Number(树状数组)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18395    Accepted Submission(s): 11168 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu1394 Minimum Inversion Number(线段树单点更新||暴力)

题目链接: huangjing 这个题目暴力和线段树都可以过,但是都需要掌握一个规律.. 当队首元素移到队尾后,可定会减少a[i]个逆序对,然后增加n-1-a[i]个逆序对. 你看比如1移到队尾,那么1>0这个逆序对就会减少,2>1,3>1,4>1这些逆序对就会增加.. 所以发现这个规律就好做了.. 暴力做法就是直接那样模拟.. 线段树做法是首先建立一颗空树,然后插入之前询问这颗树中在[插入元素,n]之间的树,求出来的就是逆序对的个数.然后将其更新进去即可.. 题目: 寻人启事:2

hdu1394 Minimum Inversion Number(最小逆序数)

Minimum Inversion Number Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 1   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description The inversion

【线段树】HDU1394 - Minimum Inversion Number

[题目大意] 给出0..n-1组成的一段数,可以移动前几个数到结尾.求出最小的逆序对个数. [思路] 先用线段树求出逆序对,方法和树状数组是一样的.然后对于当前第一个数num[0],在它之后比它小的数有num[0],则它移动到末位之后减小的逆序对是num[0],增加的是n-1-num[0]. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5

hdu1394 Minimum Inversion Number 线段树和树状数组

题意: 输入一个长度 n 第二行给出长度为n的数组,数组的值刚好为0到n-1这n个数. 然后每次把数组的第一个数放到最后一个,放n-1次,共有n个排列,这n个排列就有n个逆序数,输出这n个逆序数的最小值. 我的做法: 1.每次输入a[i]后,都把a[i] ++; 2.求出第一个排列的逆序数 3.递推求出所有的逆序数 那怎么求1呢? 对于每一个a[i],求出1到i-1 中比它大的个数,然后相加,即可得.若用朴素的查找,肯定会超时的,所以这里就利用线段树或者树状数组来快速查找. 线段树版本: 1 #

【HDU1394】Minimum Inversion Number(线段树)

大意:n次操作原串查询逆序数,求出所有串中最小的逆序数. 求逆序数属于线段树的统计问题,建立空树,每次进行插点时进行一次query操作即可.n次操作可以套用结论:如果是0到n的排列,那么如果把第一个数放到最后,对于这个数列,逆序数是减少a[i],而增加n-1-a[i]. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 const int maxn = 5e3 + 10; 6 int sumv[

HDU1394(Minimum Inversion Number)

题目地址:Minimum Inversion Number 题目大意: 求逆序对数,求循环移位后逆序数的最小值,意思一次将第一位移到最后一位,然后计算逆序对数,求出最小的那个. 解题思路: 数组数组. 线段树代码. 代码: 1 #include <algorithm> 2 #include <iostream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstring> 6 #in