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. remove() 方法的兼容问题

    一直以为jq的remove()方法是兼容的,今天才发现,原来ie的写法不一样,特作此记录. removeNode方法的功能是删除一个节点,语法为node.removeNode(false)或者node ...

  2. view--4种Android获取View宽高的方式

    有时我们会有基于这样的需求,当Activity创建时,需要获取某个View的宽高,然后进行相应的操作,但是我们在onCreate,onStart中获取View的大小,获取到的值都是0,只是由于View ...

  3. Codeforces724D [字符串][乱搞][贪心]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意:给你一个区间长度n和一个字符串,要求在字符串中选择一些symbol使得字符串的任意长度为n的子区间都存在至少一个symbol. 任意选取symbol ...

  4. CoolTrayIcon4.0

    CoolTrayIcon:在任务栏放置图标的控件,是同类空间中功能最为完善和强大的. 1.支持动态图标 2.交互式气球样式的提示框 3.支持bitmaps到icons的转换 4.支持设计状态预览 5. ...

  5. c# 串口编程

    http://news.ccidnet.com/art/32859/20100524/2067861_4.html 字节缓冲器处理类: /// <summary> /// 字节缓冲器 // ...

  6. node js 调试

    npm install -g node-inspector node --debug app.js >重新打开一个窗口   node-inspector &   KO!       no ...

  7. wget cooikes 下载

    2.下来用wget带cookie的命令下载,命令如下: wget -c –load-cookies=cookies.txt  ”下载地址” -O “文件名” &       [文件名处自己命名 ...

  8. ajax实现文件下载

    前台: <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> ...

  9. Operate blob data in Oracle via C#

    oracle table: CREATE TABLE "SCOTT"."TEST_BLOB"    (    "NAME" VARCHAR2 ...

  10. SQL 基础:Select语句,各种join,union用法

    一.基本的SELECT语句 1. “*”的注意事项:在SELECT语句中,用*来选取所有的列,这是一个应该抵制的习惯. 虽然节省了输入列名的时间,但是也意味着获得的数据比真正需要的数据多的多.相应的, ...