Minimum Inversion Number

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

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
逆序数的概念:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那末它们就称为一个逆序。
一个排列中逆序的总数就称为这个排列的逆序数。
思路:先暴力求出开始时的逆序数,然后会有一个性质:每次把末尾的数掉到序列前面时,减少的逆序对数为n-1-a[i] ,增加的逆序对数为a[i] ,所以求出其他情况时的逆序数。
 #include<cstdio>

 int t[];
int n,ans,sum;
int main()
{
while (scanf("%d",&n)!=EOF)
{
sum = ;
for (int i=; i<=n; ++i)
scanf("%d",&t[i]);
for (int i=; i<n; ++i)
for (int j=i+; j<=n; ++j)
if (t[i]>t[j]) sum++;
ans = sum;
for (int i=n; i>=; --i)
{
sum -= n--t[i];
sum += t[i];
if (sum < ans) ans = sum;
}
printf("%d\n",ans);
}
return ;
}

1394-Minimum Inversion Number的更多相关文章

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

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

  2. 逆序数2 HDOJ 1394 Minimum Inversion Number

    题目传送门 /* 求逆序数的四种方法 */ /* 1. O(n^2) 暴力+递推 法:如果求出第一种情况的逆序列,其他的可以通过递推来搞出来,一开始是t[1],t[2],t[3]....t[N] 它的 ...

  3. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  4. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  5. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

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

  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. hdu 1394 Minimum Inversion Number 逆序数/树状数组

    Minimum Inversion Number Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...

  8. hdu 1394 Minimum Inversion Number(逆序数对) : 树状数组 O(nlogn)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394  //hdu 题目   Problem Description The inversion number ...

  9. HDU 1394——Minimum Inversion Number——————【线段树单点增减、区间求和】

    Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  10. HDU 1394 Minimum Inversion Number(最小逆序数 线段树)

    Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...

随机推荐

  1. SONA Topology

    N多年以前就有有人设计传了一种类似“房子”状结构的拓扑图,在Cisco的文档中可以查到这种叫SONA.这是个非常神奇的设计,适合用于中小型网络,之所以这么讲,是因为在这个结构下,但凡任何一台接入层或者 ...

  2. 长大Tips

    队名:CW 队员: B20150304403 王香辉 B20150304408 李孟君 B20150304411 曾翡 B20150304414 吴海波 B20150304430 文淼 B201503 ...

  3. GCO团队合作

    队名:GCO 队员:   B20150304116谢冰媛 (组长) B20150304401王粲 B20150304115钟玺琛            B20150304226梁天海          ...

  4. C语言 变量类型

    // a是一个全局变量,静态变量 int a; void test() { // b是一个局部变量,自动变量 ; b++; // c是一个局部变量,静态变量 ; c++; printf("b ...

  5. asp.net反射的运用

    反射的用途:    (1)使用Assembly定义和加载程序集,加载在程序集清单中列出模块,以及从此程序集中查找类型并创建该类型的实例.     (2)使用Module了解包含模块的程序集以及模块中的 ...

  6. groupdel

    功能说明:用于删除指定的用户组,此命令不能删除用户归属的主用户组.

  7. Android学习笔记_51_转android 加载大图片防止内存溢出

    首先来还原一下堆内存溢出的错误.首先在SD卡上放一张照片,分辨率为(3776 X 2520),大小为3.88MB,是我自己用相机拍的一张照片.应用的布局很简单,一个Button一个ImageView, ...

  8. 123. Best Time to Buy and Sell Stock III ——LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. 在文件中的AngularJS模块

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. swiper插件使用技巧

    1.加载插件: <!DOCTYPE html> <html> <head> ... <link rel="stylesheet" href ...