Minimum Inversion Number

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

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
 
Author
CHEN, Gaoli
 
Source
 
Recommend
Ignatius.L
 
题解:这题的维护信息为每个数是否已经出现。每次输入后,都从该点的值到n-1进行查询,每次发现出现了一个数,由于是从该数的后面开始找的,这个数肯定是比该数大的。那就是一对逆序数,然后逆序数+1.最后求完所有的逆序数之后,剩下的就可以递推出来了。因为假如目前的第一个数是x,那当把他放到最后面的时候,少的逆序数是本来后面比他小的数的个数。多的逆序数就是放到后面后前面比他大的数的个数。因为所有数都是从0到n-1.所以比他小的数就是x,比他大的数就是n-1-x。这样的话每次的逆序数都可以用O(1)的时间计算出来。然后找最小的时候就可以了。
 
 #include<cstdio>
#include<algorithm> using namespace std; #define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1 const int maxn = + ;
int sum[maxn<<]; void PushUp(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
} void build(int l,int r,int rt)
{
int m; sum[rt]=;
if(l==r) {
return;
}
m=(l+r)>>;
build(lson);
build(rson);
} int query(int L,int R, int l, int r,int rt)
{
int m,ret; if(L<=l && r<=R) {
return sum[rt];
} m=(l+r) >> ;
ret=;
if(L<=m) ret+=query(L,R,lson);
if(R>m) ret+=query(L,R,rson); return ret;
} void Updata(int p,int l,int r,int rt)
{
int m;
if (l==r) {
sum[rt]++;
return ;
}
m=(l+r)>>;
if(p<=m) Updata(p,lson);
else Updata(p,rson); PushUp(rt);
} int main()
{
int n,sum,x[maxn]; while(~scanf("%d",&n)) {
sum=;
build(,n-,); for(int i = ;i < n;i++) {
scanf("%d",&x[i]);
sum+=query(x[i],n-,,n-,);
Updata(x[i],,n-,);
}
int ret=sum;
for(int i=;i<n;i++) {
sum+=n-x[i]-x[i]-;
ret=min(ret,sum);
} printf("%d\n",ret);
} return ;
}

[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 线段树

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

  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_线段树求逆序数

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

  6. hdu 13394 Minimum Inversion Number 线段树

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

  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 (树状数组)

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

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

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

随机推荐

  1. CCI_chapter 4 trees and Grapths

    4.1Implement a function to check if a tree is balanced For the purposes of this question,a balanced ...

  2. About Undefined Behavior[译文]

    原文:blog.llvm.org/2011/05/what-every-c-programmer-should-know.html 人们偶尔会问为什么LLVM的汇编代码有时会在优化器打开时产生SIGT ...

  3. 数据库范式(1NF 2NF 3NF BCNF)详解

    数据库的设计范式是数据库设计所需要满足的规范,满足这些规范的数据库是简洁的.结构明晰的,同时,不会发生插入(insert).删除(delete)和更新(update)操作异常.反之则是乱七八糟,不仅给 ...

  4. 2015第24周二Spring事务2

    今天继续深入学习SPring事务,发现网上很多文章都是很相似的转载没多少价值,就觉得更有必要把这个主题深入下去,先是摘录那些对自己有用的观点,后期再结合源码进行全面的整理. Spring提供了许多内置 ...

  5. linux下core文件设置(转)

    在程序不寻常退出时,内核会在当前工作目录下生成一个core文件(是一个内存映像,同时加上调试信息).使用gdb来查看core文件,可以指示出导致程序出错的代码所在文件和行数. 1.core文件的生成开 ...

  6. 【POJ2242】The Circumference of the Circle(初等几何)

    已知三点坐标,算圆面积. 使用初等几何知识,根据海伦公式s = sqrt(p(p - a)(p - b)(p - c)) 和 外接圆直径 d = a * b * c / (2s) 来直接计算. #in ...

  7. Java 四大域对象总结

    一.ServletContext 1.生命周期:当Web应用被加载进容器时创建代表整个web应用的ServletContext对象,当服务器关闭或Web应用被移除时,ServletContext对象跟 ...

  8. SVN 一次性提交多个目录中文件

    情况一:将项目中未加入版本控制的文件提交到版本库. 在使用WINDOW下的SVN客户端工具时,在提交一个项目的文件时,如果有未加入版本库的文件,这时可以先将未加入的文件选中,然后一起提交. 但在LIN ...

  9. hdu 2818 Building Block (带权并查集,很优美的题目)

    Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...

  10. Web Service 初步了解

    Web Service见名之意就是网络上的一些服务,解决的问题就是如何使用这些服务,因为软件的开发有各种各样的语言,利用Java,C#,VB.NET,PHP等等,如何使这些语言编写的程序能够进行互通, ...