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

先百科一下逆序数的概念: 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个 逆序 。一个排列中逆序的总数就称为这个排列的 逆序数 。逆序数为 偶数 的排列称为 偶排列 ;逆序数为奇数的排列称为奇排列 。如2431中,21,43,41,31是逆序,逆序数是4,为偶排列。

逆序数计算方法是:在逐个元素读取原始数列时,每次读取都要查询当前已读取元素中大于当前元素的个数,加到sum里,最后得到的sum即为原始数列的逆序数。

现在来说一下求解思路:

输入数组时,每输入一个数,就for(j=0;j<i-1;j++)比较大小,这样用sum把逆序数统计出来,其实这里才是暴力,至于后面的就很巧妙,公式很容易推出,因为题目总是把第一个数移到最后一个位置,所以原来比它小的数(和它构成逆序)在移动之后就不是逆序了,而原来比它大的数(不和它构成逆序)在移动之后就是逆序了,这样sum就变化了:Sum=sum-(low[a[i]])+(up[a[i]]); 显然在序列0,1,2,…..n-1中比a[i]小的数的个数是 Low[a[i]]=a[i];  比a[i]大的数的个数是up[a[i]]=n-a[i]-1; 题目要求是循环移动n次,那么只要写个for,把a[0],a[1],a[2]……a[n-1]都移动一遍,sum进行n次上面的公式运算,同时记录最小值,就是最小逆序数了。

接下来是本馒头暴力AC的代码,数据为5000时的耗时为109ms,应该庆幸数据小= =

#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std; #define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 const int MX = 5050;
int num[MX];
int n; int main() {
//freopen("input.txt", "r", stdin);
while (scanf("%d", &n) != EOF) { int sum = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &num[i]); for (int j = 0; j < i; j++) {
if (num[j] > num[i]) sum++;
}
}
int Min = INF;
for (int i = 0; i < n; i++) { sum = sum - num[i] + n - num[i] - 1;
Min = min(sum, Min);
} printf("%d\n", Min);
}
return 0;
} 由于最近在学习线段树,所以这道题也用线段树优化一下,主要是优化算出sum的那一段,使得整体算法时间复杂度减低到nlongn,对数默认以二为底,速度优化到62ms,以下是AC代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std; #define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 const int MX = 5050;
int num[MX];
int n;
int sum[MX<<2]; void push_up(int rt) {
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void build(int l, int r, int rt) {
if (l == r) {
sum[rt] = 0;
return ;
}
int m = (l + r)>>1;
build(lson);
build(rson);
push_up(rt);
}
void update(int pos, int l, int r, int rt) {
if (l == r) {
sum[rt]++;
return ;
}
int m = (l + r)>>1;
if (pos <= m) update(pos, lson);
else update(pos, rson);
push_up(rt);
}
int query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) {
return sum[rt];
}
int m = (l + r)>>1;
int ret = 0;
if (L <= m) ret += query(L, R, lson);
if (R > m) ret += query(L, R, rson);
return ret;
}
int main() {
//freopen("input.txt", "r", stdin);
while (scanf("%d", &n) != EOF) {
memset(sum, 0, sizeof(sum));
int s = 0;
build(1, n, 1);
for (int i = 0; i < n; i++) {
scanf("%d", &num[i]);
update(num[i] + 1, 1, n, 1);
s += query(num[i] + 2, n, 1, n, 1);
}
int Min = INF;
for (int i = 0; i < n; i++) { s = s - num[i] + n - num[i] - 1;
Min = min(s, Min);
} printf("%d\n", Min);
}
return 0;
}
想起来再慢慢看,先写点东西堆在这里,嘻嘻

HDU-Minimum Inversion Number(最小逆序数)的更多相关文章

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

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

  2. hdu1394 Minimum Inversion Number(最小逆序数)

    Minimum Inversion Number Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/O ...

  3. hdu Minimum Inversion Number(逆序数的小知识与线段树)

    飞! 题解 首先,求逆序数对的思路: 1.得到整个数列后,从前往后扫,统计比a[i]小的,在a[i]后面的有多少个 这样做的话,应该是只有n2的暴力作法,没想到更好的方法 2.统计a[i]前面的,且比 ...

  4. HDU 1394.Minimum Inversion Number-最小逆序数-完全版线段树(单点增减、区间求和)

    HDU1394.Minimum Inversion Number 这个题求最小逆序数,先建一个空的树,然后每输入一个值,就先查询一下,查询之后,更新线段树,然后遍历一遍,每次将第一个数放到最后之后,减 ...

  5. hdu 1394(线段树) 最小逆序数

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 给出一列数组,数组里的数都是从0到n-1的,在依次把第一个数放到最后一位的过程中求最小的逆序数 线段树的应 ...

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

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

  7. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  8. hdu 1394 Minimum Inversion Number 逆序数/树状数组

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

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

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

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

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

随机推荐

  1. Algorithms, Part I by Kevin Wayne, Robert Sedgewick

    Welcome to Algorithms, Part I 前言 昨天在突然看到了Coursera上Robert Sedgewick讲的Algorithms,Part II看了一些,甚是爽快,所以又去 ...

  2. Ubuntu下Chromium for Android 源码的编译

    转自:http://blog.csdn.net/leer168/article/details/9146689 一.环境Ubuntu10.4.4 -desktop-amd64 + VMware Wor ...

  3. HDU 4251 The Famous ICPC Team Again 主席树

    The Famous ICPC Team Again Problem Description   When Mr. B, Mr. G and Mr. M were preparing for the ...

  4. 移动 Web 开发技巧

    1.使用click会出现绑定点击区域闪一下的情况,解决:给该元素一个样式如下 -webkit-tap-highlight-color: rgba(0,0,0,0); 2.用iphone或ipad浏览很 ...

  5. supervisor(三)xml_rpc

    supervisor提供的两种管理方式,supervisorctl和web其实都是通过xml_rpc来实现的. xml_rpc其实就是本地可以去调用远端的函数方法,然后函数方法经过一番处理后,把结果返 ...

  6. ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...

  7. SQL初级第二课

    随着我们数据库越来越复杂 我们要掌握的姿势也要也来越多.... 首先建立个表 create table shop(code int primary key identity (1,1),name va ...

  8. Spring mvc 验证码的做法

    http://jingyan.baidu.com/article/4f7d5712da7a131a201927b0.html

  9. 后缀数组 POJ 1743 Musical Theme

    题目链接 题意:给定n个数字,求超过5个数字的,最长的,变化相同的,不相交的重复子串 分析:男人8题中的一题!数列相邻两项做差,形成新数列,即求数列中的最长重复子串(不可相交). 后缀数组+二分答案. ...

  10. Experimental Educational Round: VolBIT Formulas Blitz

    cf的一次数学场... 递推 C 题意:长度<=n的数只含有7或8的个数 分析:每一位都有2种可能,累加不同长度的方案数就是总方案数 组合 G 题意:将5个苹果和3个梨放进n个不同的盒子里的方案 ...