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. X5的UI部分和传统Web页面开发的差异

    http://doc.wex5.com/different-with-std-web-ui/#1 X5的UI部分和传统Web页面开发的差异 WeX5是跨端移动开发框架,BeX5是基于WeX5的企业快速 ...

  2. android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )屏幕适配

    http://www.tuicool.com/articles/nuyMZb 1 Android手机目前常见的分辨率 1.1 手机常见分辨率: 4:3 VGA     640*480 (Video G ...

  3. linux 下 用户与用户组

    1,增加一个test组:groupadd test 2,将test组重命名test2:groupmod -n test2 test 3,删除test2组:groupdel test2 4,查看当前登录 ...

  4. (C#) 基本概念一览表

    A abstract class An abstract class is a class that must be inherited and have the methods overridden ...

  5. 字符串连接,数字tostring,写入文件

    #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int x=2410; in ...

  6. [家里蹲大学数学杂志]第049期2011年广州偏微分方程暑期班试题---随机PDE-可压NS-几何

    随机偏微分方程 Throughout this section, let $(\Omega, \calF, \calF_t,\ P)$ be a complete filtered probabili ...

  7. [翻译]用神经网络做回归(Using Neural Networks With Regression)

    本文英文原文出自这里, 这个博客里面的内容是Java开源, 分布式深度学习项目deeplearning4j的介绍学习文档. 简介: 一般来说, 神经网络常被用来做无监督学习, 分类, 以及回归. 也就 ...

  8. JAVA的包装类 【转】

    Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,这在实际使用时存在很多的不便,为了解决这个不足,在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数 ...

  9. 网络存储技术介绍(1) ( based on zt)

    最近由于某同学微信发了一些网络存储的文章,开始感兴趣,稍微收集了一些 一.  网络存储技术 http://ask.zol.com.cn/q/187044.html  (yxr:很老的技术介绍吧) 网络 ...

  10. 在.net桌面程序中自定义鼠标光标

    有的时候,一个自定义的鼠标光标能给你的程序增色不少.本文这里介绍一下如何在.net桌面程序中自定义鼠标光标.由于.net的桌面程序分为WinForm和WPF两种,这里分别介绍一下. WinForm程序 ...