HDU--1394 Minimum Inversion Number

来源:互联网 发布:360软件还有缺点 编辑:程序博客网 时间:2024/05/14 01:08

Minimum Inversion Number

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


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
101 3 6 9 0 8 5 7 4 2
 

Sample Output
16
 

Author
CHEN, Gaoli
 

Source
ZOJ Monthly, January 2003

题目有点难懂,大意如下:给你一个序列,每次将第一个数转移至最后,求每次转移前的逆序数并找出最小的那个逆序数
逆序数定义:
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数



线段树的代码贴上:
/** 线段树求逆序数原理: *线段树叶节点的表示的区间(其实就是一个点)就是要存入数字的值 *用线段树的叶节点表示序列中数的存储状态,1表示已经存入,0表示未存入 *每次读取一个数值x[i]后,先查询线段树中从x[i]到n - 1的区间中已经存入了多少数,该值就是该数值的逆序数 *将每个数的逆序数累加后的和就是当前序列的逆序数 *注意查询是在存入数据之前 *求出初始序列的逆序数就可以按照规律推出每次转移第一个数后的逆序数了 */#include<iostream>#include<cstdio>#include<cstring>#include <cmath>#define N 5005using namespace std;int Tree[N << 2];void PushUp(int rt){    Tree[rt] = Tree[rt << 1] +  Tree[rt << 1 | 1];}void build(int l,int r,int rt)   //线段树建立,节点全部为0{    int mid;    Tree[rt] = 0;    if(l == r)    {        return;    }    mid = (l + r ) >> 1;    build(l,mid,rt << 1);    build(mid + 1,r,rt << 1 | 1);}void Update(int p,int l,int r,int rt)//更新节点值,用于记录存入的数字的位置{    int mid;    if(l == r)    {        Tree[rt]++;//1代表数字已经记录,0代表该数字还未存入        return;    }    mid = (l + r) >> 1;    if(p <= mid)    {        Update(p,l,mid,rt << 1);    }    else    {        Update(p,mid + 1,r,rt << 1 | 1);    }    PushUp(rt);}int   Query(int ll,int rr,int l,int r,int rt)//求ll到rr区间之间的和,该结果就是当前数字的逆序数{    int mid;    int ret = 0;    if(ll <= l && r <= rr)    {        return Tree[rt];    }    mid = (l + r) >> 1;    if(ll <= mid) ret += Query(ll,rr,l,mid,rt << 1);    if(rr > mid) ret += Query(ll,rr,mid + 1,r,rt << 1 | 1);    return ret;}int x[N];int main(){    int n;    int sum,ret;    int i;    //freopen("FileIn.txt","r",stdin);    //freopen("FileOut.txt","w",stdout);    while(scanf("%d",&n) != EOF)    {        build(0,n - 1,1);//序列从0开始,所以l = 0,r = n - 1;        sum = 0;        for(i = 0; i < n; i++)        {            scanf("%d",&x[i]);            sum += Query(x[i],n -1 ,0,n - 1,1);//求大于x【i】的数字个数            Update(x[i],0,n - 1,1);        }        ret = sum;//最开始序列的逆序数        for(i = 0; i < n; i++)        {            sum += n - x[i] - x[i] - 1;//每次将第一个数移到最后,逆序数增加n-1-x[i]个的同时减少x[i]个(因为该序列从0开始)            ret = min (ret,sum);//最终结果要取最小值        }        printf("%d\n",ret);    }    //fclose(stdin);    //fclose(stdout);    return 0;}



0 0
原创粉丝点击