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. php复制目录

    function copyDir($dirSrc,$dirTo) { if(is_file($dirTo)) { echo '目标不是目录不能创建!'; return; } if(!file_exis ...

  2. Socket 简易静态服务器 WPF MVVM模式(一)

    整体代码下载 主要实现功能: Socket的简单应用 可修改IP和端口 显示来访信息 界面设计: 界面采用MVVM设计,很简陋. 前台的主要目的是 输入IP地址 输入端口 输入文件目录 开启监听和停止 ...

  3. iOS开发系统类功能划分

    0.OC语法基础 CHOCBase Object C语法学习笔记(一) Object C语法学习笔记(二) 1.UI类 自定义控件程序运行流程 setNeedsLayOut和setNeedsDispl ...

  4. javaee--学生成绩录入与显示--Struts2标签的使用

    类Score.java:各课程的成绩及平均成绩 类Student.java:学生姓名.学号及Score类 类ScoreAction.java:将Student类存在一个List对象中, execute ...

  5. 关于Java中SQL语句的拼接规则

    实现目标语句是这个注意,这里的java变量是idd int idd; String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEE ...

  6. js new关键字 和 this详解

    构造函数 ,是一种特殊的函数.主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中. 构造函数用于创建一类对象,首字母要大写. 构造函数要和new一起 ...

  7. rest-assured的对象映射(序列化和反序列化)

    rest-assured支持映射Java对象到Json和XML以及从Json和XML中映射到Java对象.Json映射需要在classpath 中有Jackson.Jackson 2或者是Gson,X ...

  8. POJ_1862 Stripies 【贪心】

    一.题面 POJ1862 二.分析 反省一下,自己英语水平着实不行,该题其实就是问若给出若干个这种生物,根据这种体重变换方式,最终合并成一个后,体重最少是多少.根据公式 $m = 2\sqrt{m_{ ...

  9. Linux 安装 python3

    1. 安装依赖环境 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline- ...

  10. 方格填数--蓝桥杯---dfs

    答案:1580 相似题目:N皇后问题 注意要枚举的是什么 #include<iostream> #include<string.h> using namespace std; ...