Minimum Inversion Number

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

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   |   We have carefully selected several similar problems for you:  1166 1698 1540 1542 1255 
 
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394

题意:求出对(ai,aj)(i<j,ai>aj)的全部数量。即求ai右边比ai小的数的个数和。每次变换a的序列,求出最小的逆序对数量和。
思路: 因为树状数组的最基本功能就是求比某点 x 小的点的个数。所以逆向存储ai。
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=1e6+,INF=1e9+;
int a[MAXN],c[MAXN],ans[MAXN];
int lowbit(int x)
{
return x&(-x);
}
void add(int i,int val)
{
for(i; i<=MAXN; i+=lowbit(i))
c[i]+=val;
}
int sum(int i)
{
int s=;
for(i; i>; i-=lowbit(i))
s+=c[i];
return s;
}
int main()
{
int i,j,t,n;
while(scanf("%d",&n)!=EOF)
{
for(i=n; i>=; i--)
scanf("%d",&a[i]);
memset(c,,sizeof(c));
memset(ans,,sizeof(ans));
int cou=;
for(i=; i<=n; i++)
{
ans[i]=sum(a[i]+);
cou+=ans[i];
add(a[i]+,);
}
int Min=cou;
for(i=n; i>; i--)
{
for(j=; j<=n; j++)
{
if(j==i) continue;
if(a[i]<a[j])
{
cou++;
ans[j]++;
}
}
cou-=ans[i];
ans[i]=;
if(cou<Min) Min=cou;
}
cout<<Min<<endl;
}
return ;
}

逆序对

HDU 1394Minimum Inversion Number 数状数组 逆序对数量和的更多相关文章

  1. [树状数组+逆序对][NOIP2013]火柴排队

    火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...

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

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

  4. HDU 1394 Minimum Inversion Number (树状数组 && 规律 && 逆序数)

    题意 : 有一个n个数的数列且元素都是0~n-1,问你将数列的其中某一个数及其前面的数全部置到后面这种操作中(比如3 2 1 0中选择第二个数倒置就产生1 0 3 2)能产生的最少的逆序数对是多少? ...

  5. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  6. hdu 1394 Minimum Inversion Number - 树状数组

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

  7. [hdu1394]Minimum Inversion Number(树状数组)

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

  8. HDU 1394Minimum Inversion Number

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

  9. HDU 2689Sort it 树状数组 逆序对

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

随机推荐

  1. c#上iOS apns p12文件制作记录 iOS推送证书制件

    前期一些准备工作可参考:http://jingyan.baidu.com/article/7082dc1c6bb86de40a89bd1a.html 1.在桌面上建一个"apns_p12&q ...

  2. mysql 的 infobright 数据库的 mediumblob 显示不了数据

    需要修改mysql的配置文件: /var/www/html/phpmyadmin/config.inc.php 增加: $cfg['ProtectBinary'] = FALSE; 即可

  3. 黄聪:PHP7.0中htmlspecialchars出错解决方案(wordpress)

    htmlspecialchars( $context, ENT_COMPAT, 'UTF-8' )

  4. [转]理解android.intent.category.LAUNCHER 具体作用

    转自:http://blog.csdn.net/jackrex/article/details/9189657 android.intent.category.LAUNCHER 具体有什么作用?我做一 ...

  5. (C#) Tasks 中的异常处理(Exception Handling.)

    多线程编程中要注意对线程异常的处理.首先写个例子. 一个线程用于显示信息(Show Messages).主线程用于做其他工作(Do Works). using (Task taskShowMessag ...

  6. sql server 相似度对比

    转自:http://www.dotblogs.com.tw/rachen/archive/2008/10/07/5611.aspx 函數一.產生 Like 比對用字串 ) ) ) as begin / ...

  7. gerrit 修改前一次提交的方法(转载)

    From:http://sinojelly.sinaapp.com/2011/08/git-changes-submitted-by-the-previous-method-pay-special-a ...

  8. (转)pdf文件结构

    PDF文件结构(一)  ————物理结构 PDF(Portable   Document   Format,便携式文档结构)是一种很有用的文件格式,其最大的特点是平台无关而且功能强大(支持文字/图象/ ...

  9. 清理Oracle安装目录里的一些日志信息

    最近测试环境服务器上磁盘空间紧张,表空间里面的数据也不知道开发哪些需要哪些不需要,而且因为此测试库运行时间比较久远,起码有三年了.于是就从清理Oracle安装目录下的日志下手. 发现在一般这几个日志, ...

  10. [转载]:fortran之format格式化输出总结

    先贴一段别人总结好的: 格式化输出的控制字符非常的丰富,但常用的并不多,一般说来:" I .F.E.A.X "是最常使用的几个格式,最 好把它们都记下来. Iw[.m] 以w个字符 ...