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. JSP(一)

    一.JSP概要 一]JSP的概念 1>JSP是SUN公司开发的一个基于服务端的一种动态WEB开发技术.         2>JSP的代码结构/内容 = HTML内容+JSP特有元素内容   ...

  2. PowerShell 中使用json对象的性能比较

    PowerShell v3 – Creating Objects With [pscustomobject] – it’s fast! September 19, 2011powershell, v3 ...

  3. ActionBarActivity & FragmentActivity

    1 ActionBarActivity 是FragmentActivity的一个子类 2 ActionBarActivity 加入了对actionBar的操作, 比如getSupportActionB ...

  4. 深入浅出Node.js (8) - 构建Web应用

    8.1 基础功能 8.1.1 请求方法 8.1.2 路径解析 8.1.3 查询字符串 8.1.4 Cookie 8.1.5 Session 8.1.6 缓存 8.1.7 Basic认证 8.2 数据上 ...

  5. 源码分析之spring-JdbcTemplate日志打印sql语句

    对于开源的项目来说的好处就是我们遇到什么问题可以通过看源码来解决. 比如近期有个同事问我说,为啥JdbcTemplate中只有在Error的时候才打印出sql语句呢.我一想,这和log的配置有关系吧. ...

  6. NoSQL 数据库的使用场景

    摘要:对比传统关系型数据库,NoSQL有着更为复杂的分类——键值.面向文档.列存储.图数据库.这里就带你一览NoSQL各种类型的适用场景及一些知名公司的方案选择. 在过去几年,关系型数据库一直是数据持 ...

  7. 使用android-resource-remover删除项目中无用的资源,减少包的大小

    写这篇文章的原因是,一个CSDN的资源链接,Android程序员必备精品资源,在该链接的实用工具集锦中有一个工具吸引了我的注意,那就是android-resource-remover,它的解释是:一个 ...

  8. NFinal 视图—模板

    创建模板 1.新建Header.ascx用户控件,此控件就是模板,修改内容如下: <%@ Control Language="C#" AutoEventWireup=&quo ...

  9. 如何禁用不需要的HTTP方法

    IIS7.0默认开启了不安全的OPTIONS和TRACE方法,建议关闭这两个方法. 以下环境为windows server 2008.IIS7.0 方法(1):web.config 在<conf ...

  10. Chinese_PRC_CI_AS and SQL_Latin1_General_CP1_CI_AS类型错误

    在编写存储过程时,经常会用到临时表,而且往往会使用临时表与正式表相关联. 当临时表与正式表关联的字段是字符类型的时候,因为临时表创建的字符类型会默认数据库的编码,而正式表的字符编码可能不同,那么这个时 ...