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

来源:互联网 发布:袋鼠云 知乎 编辑:程序博客网 时间:2024/05/18 14:26

点击打开链接Minimum Inversion Number

有一点没想明白,今天突然间想明白了,sum[rt]记录的是rt表示的区间有几个数,而输入一个数,查询一次,查询的目的就是返回当前区间内已经输入了多少个数,查询区间是(x,n-1)是因为要找总共有多少个逆序数,比如说先输入5,再输入3,那么输入3时查询前边输入的数有几个比三大的即当前这些数的总的逆序数,因为输入3,前边找到一个比3大的数,那么就看5,3,可以知道,5由于3的输入,逆序数加1,所以有用

ans += query(x[i] , n- 1 , 0 , n - 1 ,1);此时ans里边记录的就是当前序列的逆序数之和

 

1、题目大意:

给定一串数字,求这一组数字的逆序数,而且这组数据可以改变,一次将前边的第一个数移到最后一个数的位置,构成新的数列,在诸多序列中,求出一个最小的逆序数

2、思路:

简单的线段树处理,单点更新即可,网上有一个很巧妙的处理最小逆序数的方法,

当把x放入数组的后面,此时的逆序数应该为x没放入最后面之前的逆序总数加上(n-x)再减去(x-1);sum = sum+(n-x[i])-(x[i]-1)。

此方法现在才明白,例如3 1 4 2 5,如果将3移到最后,那么比3大的数4,5的逆序数分别加1,即上式中的加上(n-x),而比3小的1,2的逆序数相比较以前分减1,即上式中的再减(x-1)

3、题目:

Minimum Inversion NumberTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5831    Accepted Submission(s): 3547Problem DescriptionThe 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 Input101 3 6 9 0 8 5 7 4 2 Sample Output16 AuthorCHEN, Gaoli SourceZOJ Monthly, January 2003  RecommendIgnatius.L


 

4、代码:

#include<stdio.h>#include<iostream>#include<algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int x[5005];int sum[5005*4];void build(int l,int r,int rt){    sum[rt]=0;    if(l==r) return ;    int m=(l+r)>>1;    build(lson);    build(rson);}int query(int L,int R,int l,int r,int rt){    if(L<=l&&R>=r)        return sum[rt];    int m=(l+r)>>1,ret=0;    if(L<=m)        ret+=query(L,R,lson);    if(R>=m+1)        ret+=query(L,R,rson);    return ret;}void update(int x,int l,int r,int rt){    if(l==r)    {        sum[rt]++;        return ;    }    int m=(l+r)>>1;    if(x<=m)        update(x,lson);    if(x>=m+1)        update(x,rson);    sum[rt]=sum[rt<<1]+sum[rt<<1|1];}int main(){    int n,ans;    while(scanf("%d",&n)!=EOF)    {        ans=0;        build(0,n-1,1);        for(int i=1; i<=n; i++)        {            scanf("%d",&x[i]);            ans+=query(x[i],n-1,0,n-1,1);            update(x[i],0,n-1,1);        }        int minsum=ans;        for(int i=1; i<=n; i++)        {            ans=ans+(n-x[i])-(x[i]+1);            minsum=min(ans,minsum);        }        printf("%d\n",minsum);    }    return 0;}/*101 3 6 9 0 8 5 7 4 2*/


 

原创粉丝点击