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

逆序数,就是你比人家小,结果还在人家后边。。。可以用线段树或树状数组求,每一次出现一个数,设为a,就求一次a到n之间的和,也就是比a大却先比a出现的数字个数,然后把a的位置标记为一,其余几次的逆序数可以直接推导出来,设第一次移动的数为a,把它移动到后面理论上会减少a个逆序数(注意从0开始),但是还会有n-1-a个数大于a,所以sum+=n-1-2*a;

线段树:

zkw线段树:

HDU-1394 Minimum Inversion Number (逆序数,线段树或树状数组)的更多相关文章

  1. hdu 1394 Minimum Inversion Number(逆序数对) : 树状数组 O(nlogn)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394  //hdu 题目   Problem Description The inversion number ...

  2. hdu 1394 Minimum Inversion Number 逆序数/树状数组

    Minimum Inversion Number Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...

  3. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

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

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

  5. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  6. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

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

  7. HDU 1394 Minimum Inversion Number(最小逆序数 线段树)

    Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...

  8. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  9. hdu 1394 Minimum Inversion Number(线段树之 单点更新求逆序数)

    Minimum Inversion Number                                                                           T ...

  10. HDU - 1394 Minimum Inversion Number (线段树求逆序数)

    Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs ( ...

随机推荐

  1. CF506E Mr. Kitayuta's Gift

    这道题神仙到让我面临着买不到冰皮月亮蛋糕的风险来写题解 (蛋糕真好吃呜呜呜) 这篇题解参考了CQzhangyu神仙的做法. (目测比标程科学好写) 限制是要回文,根据我们做字符串计数的常识,一定是尽量 ...

  2. css盒子模型中的border属性

        认识border属性 我们可以通过boder属性来为元素设置边框:元素的边框 (border) 是围绕元素内容和内边距的一条或多条线.CSS border 属性允许你规定元素边框的样式.宽度和 ...

  3. 最近在写一些树上的东西,先发一波LCA的吧!

    不会树剖的我只有去学tarjan和倍增了,个人觉得倍增比tarjan好打一点... tarjan学习的地方 http://www.cnblogs.com/JVxie/p/4854719.html 个人 ...

  4. 进阶3: zookeeper-3.4.9.tar.gz和hbase-1.2.4-bin.tar.gz 环境搭建(hbase 伪分布式)

    前提条件: 成功安装了  jdk1.8,  hadoop2.7.3 注意条件: zookeeper,hbase 版本必须要和hadoop 安装版本相互兼容,否则容易出问题: 本次:安装包 zookee ...

  5. Educational Codeforces Round 16 E. Generate a String (DP)

    Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...

  6. 获取项目配置的常用方法(Struts/Servlet)

    struts: //web.xml中: <context-param> <param-name>paramName</param-name> <param-v ...

  7. Otto

    导入依赖:implementation 'com.squareup:otto:1.3.8'1定义一个类继承Bus,并且设置单列模式注册和声明订阅者发送事件,最后解除注册与EventBus相同Event ...

  8. Linux shell】grep命令精确匹配字符串查找

    需求: 精确匹配查找某个字符串   精确匹配: 例如: 在抽取字符串“48”,返回结果包含诸如484和483等包含“48”的其他字符串,实际上应精确抽取只包含48的各行. 使用grep抽取精确匹配的一 ...

  9. Linux shell - ps,wc命令用法

    例1. 查看Oracle数据库活动进程LOCAL=NO,输出行数 oracle@sha> ps -ef|grep LOCAL=NO|wc -l 15 解释:ps -ef是查看所有的进程的 然后用 ...

  10. react-native modal

    1.属性 animationType(动画类型) PropTypes.oneOf([‘none’, ‘slide’, ‘fade’]) none:没有动画 slide:从底部滑入 fade:淡入视野 ...