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

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
  
  
10 1 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相当于多加了1
const 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;
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值