Minimum Inversion Number

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description: 
System Crawler  (2015-03-30)

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
 
 
解题思路:1.求初始序列的逆序数 2.根据初始序列逆序数,递推出下一个序列的逆序数
  1.当遍历到当前序列元素时,查询从该元素到n-1这段区间内的逆序和,即求大于该元素的元素已经出现几个(跟逆序的求法相逆,但是结果相同)。然后更新该结点及其父亲结点。遍历到结束,可求出该序列的逆序。
  2.由于是从0开始连续的数求逆序,所以,有性质即从序列头部拿走a,相当于原序列的逆序减少a;放在尾部,相当于序列的逆序增加n-1-a;于是可以递推求出题目要求的所有序列的逆序。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn=5500;
int num[maxn*4];
void PushUP(int rt){ num[rt]=num[rt*2]+num[rt*2+1];
}
void build(int rt,int L,int R){ num[rt]=0;
if(L==R)
return ;
build(lson);
build(rson);
}
int query(int rt,int L,int R,int l_ran,int r_ran){ if(l_ran<=L&&R<=r_ran){ return num[rt];
}
int ret=0;
if(l_ran<=mid){ ret+=query(lson,l_ran,r_ran);
}
if(r_ran>mid){ ret+=query(rson,l_ran,r_ran);
}
return ret;
}
void update(int rt,int L,int R,int pos){ if(L==R){ num[rt]++;
return ;
}
if(pos<=mid){ update(lson,pos);
}
if(pos>mid){ update(rson,pos);
}
PushUP(rt);
}
int main(){ int n;
while(scanf("%d",&n)!=EOF){ build(1,0,n-1);
int sum=0;
int a[5050];
for(int i=0;i<n;i++){ scanf("%d",&a[i]);
int tmp=query(1,0,n-1,a[i],n-1);
sum+=tmp;
update(1,0,n-1,a[i]);
}
int ans=sum;
for(int i=0;i<n;i++){ sum=sum+n-2*a[i]-1;
if(ans>sum){
ans=sum;
}
}
printf("%d\n",ans);
}
return 0;
}

  

 

HDU 1394——Minimum Inversion Number——————【线段树单点增减、区间求和】的更多相关文章

  1. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  2. hdu - 1394 Minimum Inversion Number(线段树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...

  3. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  4. HDU 1394 Minimum Inversion Number(线段树 或 树状数组)

    题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...

  5. HDU 1394 Minimum Inversion Number 线段树

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...

  6. HDU 1394 Minimum Inversion Number (树状数组)

    题目链接 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the numb ...

  7. hdu 1394 Minimum Inversion Number (树状数组求逆序对)

    The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...

  8. HDU 1394 Minimum Inversion Number(树状数组/归并排序实现

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  9. hdu 1394 Minimum Inversion Number(树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个0 — n-1的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...

  10. hdu 13394 Minimum Inversion Number 线段树

    题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...

随机推荐

  1. 如何彻底删除TFS上的团队项目 For VS 2017

    参考 Visual Studio 2017 TFSDeleteProject.exe 位置 X:\Program Files (x86)\Microsoft Visual Studio\2017\En ...

  2. xcode9 上传app后iTues 构建版本不显示

    1.问题原因 苹果公司更新了ios10系统和xcode9以后,做了许多调整,如果开发者没有注意就会遇到这样那样的问题.作者在更新以后就遇到了上传app到appstore成功后,没有显示的问题.下面就介 ...

  3. day3学python 字典+列表集合+文件读取

    字典+列表集合+文件读取 字典示例 ************************ 各地食品的三级菜单************************* 1.使用字典嵌套字典 2.采用死循环思路 3 ...

  4. MySQL中查询时对字母大小写的区分

    我相信很多人在mysql中查询时都遇到过mysql不区分字母大小写的情况:如以下例子: 1.SELECT * FROM `user` WHERE userpass = 'Z20'; 结果为: 2.SE ...

  5. 趣图:IT公司员工出游真实写照

      程序员调 Bug 的写照 趣图:如何辨别程序员设计师的水平

  6. php代码审计4审计代码执行漏洞

    代码执行漏洞代码执行漏洞是指应用程序本身过滤不严,用户可以通过请求将代码注入到应用中执行,当应用在调用一些能将字符串转化成代码的函数(如php中的eval)时,没有考虑到用户是否能控制这个字符串,造成 ...

  7. 洛谷P2763 试题库问题(最大流)

    传送门 网络流界的一股清流啊……终于没那么变态了…… 考虑一下怎么建图.对于每一个类型,我们从$S$向他连边,容量为它所需的题数,表明它要可以有这么多题,对于每一道题目,我们从它对应的类型向他连边,容 ...

  8. BZOJ2668:[CQOI2012]交换棋子(费用流)

    题目描述 有一个n行m列的黑白棋盘,你每次可以交换两个相邻格子(相邻是指有公共边或公共顶点)中的棋子,最终达到目标状态.要求第i行第j列的格子只能参与mi,j次交换. 输入输出格式 输入格式: 第一行 ...

  9. 公司拷贝回家的工程用sts导入clean package报错java.lang.NoClassDefFoundError

    从公司拷贝工程回家加班,用相同版本的sts和jdk但是run as    maven build   clean package 总是报错java.lang.NoClassDefFoundError: ...

  10. C++_IO与文件3-用cin进行输入

    接下来讨论的是如何给程序提供数据? cin对象将标准输入表示为字节流. 通常情况下是通过键盘来生成这种字节流 cin对象根据接收值得变量类型,使用其方法将字符序列转换为所需的类型. cin>&g ...