2013 - ECJTU 暑期训练赛第五场-problem-F

来源:互联网 发布:淘宝网夹子 编辑:程序博客网 时间:2024/05/01 06:15

Problem F

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 17   Accepted Submission(s) : 12
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
#include<iostream>//线段树做法#include<cstring>#include<cstdio>#include<string>#include<algorithm>#define Lson left,mid,n<<1//定义左孩子常量,这里n<<1相当于2*n,这里用的是C++二进制的左移,左移一位相当于乘于2#define Rson mid+1,right,n<<1|1//定义右孩子常量,这里n<<1|1相当于2*n+1,因为左移一位后空的那位是0所以逻辑或上1后就是1相当于多加了1const int Max=5001;//数据大小const int MAX=5000<<2;//节点个数int s[Max];int sum;//计算逆序数总个数using namespace std;typedef struct Node//定义节点的结构体{    int left;    int right;    int value;//每个节点价值都是1,即每个最后的子节点只有他自己};Node node[MAX];void build_tree(int left,int right,int n)//建树{    int mid;    node[n].left=left;    node[n].right=right;    node[n].value=0;    if(node[n].left==node[n].right)//如果节点的左右相等就相当于是本身,即就剩下自己这个节点    return;    mid=(node[n].left+node[n].right)>>1;//右移一位,相当于n/2    build_tree(Lson);    build_tree(Rson);}void update(int index,int n)//每次执行一次query函数即每次查询后都要更新每个节点的值{    if(node[n].left==node[n].right)    {         node[n].value=1;//找到了属于该范围内的子节点就赋值1         return;    }    int mid=(node[n].left+node[n].right)>>1;//没找到的话就从中间点向两边继续找    if(index<=mid)//如果在中间点的左边    update(index,n<<1);    else if(index>mid)//如果在中间点的右边    update(index,n<<1|1);    node[n].value=node[n<<1].value+node[n<<1|1].value;//每次都要子节点更新的值赋给父节点}void query(int left,int right,int n)//从s[i]到n-1的value的值都加起来{    if(node[n].left==left&&node[n].right==right)    {        sum+=node[n].value;        return;    }    int mid=(node[n].left+node[n].right)>>1;    if(right<mid)    query(left,right,n<<1);    else if(left>mid)    query(left,right,n<<1|1);    else    {        query(Lson);        query(Rson);    }}int main(){    int n,i,j,t,Min;    while(cin>>n)    {        build_tree(0,n-1,1);//建树        sum=0;//初始化为0        for(i=0;i<n;i++)        {            cin>>s[i];            query(s[i],n-1,1);//把s[i]到n-1的值加起来就是该节点逆序数的个数            update(s[i],1);//每次加完后要更新节点值        }        Min=sum;        for(i=0;i<n;i++)        {            sum=sum-s[i]+(n-1)-s[i];//左移序列之后得到新的序列的逆序数的个数就等与当前总的个数减左边的数的逆序数a[i],然后加上新增的逆序数n-a[i]-1            if(Min>sum)            Min=sum;        }        cout<<Min<<endl;    }    return 0;}