Minimum Inversion Number 【线段数】

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

来源: http://acm.hdu.edu.cn/showproblem.php?pid=1394

大意:给出一个从0到n-1的序列,逐次把首数字移到尾部,问,最小逆序数?

题解:
这里的逆序数和线性代数中的逆序数是一个概念,某个数前面出现比其大的数,称为逆序,总序列中逆序的个数,称为逆序数。

用线段树求第一次输入的序列的逆序数,然后用公式来遍历所有转移情况。

#include<stdio.h>
#include<string.h>
#include<string.h>
#include<algorithm>
#define M 5001
using namespace std;
struct Node{
	int a;
	int b;
	int sum;
}t[3*M];
int p[M],total;
/*
	创建范围为x~y的线段树
*/
void make(int x,int y,int n){
	t[n].a = x;
	t[n].b = y;
	t[n].sum = 0;
	if(x != y){
		int mid = (x + y)/2;
		make(x,mid,2*n);
		make(mid+1,y,2*n+1);
	}
}
/*
	返回 x~y 区间内的个数sum
*/
int query(int x,int y,int n){
	if(x <= t[n].a && y >= t[n].b){
		return t[n].sum;
	}else{
		int mid = (t[n].a + t[n].b)/2;
		if(x > mid){
			query(x,y,2*n+1);
		}else if( y <= mid){
			query(x,y,2*n);
		}else{
			return query(x,y,2*n)+query(x,y,2*n+1);
		}
	}
}
/*
	从最高级区间开始往下面的具有x的区间的sum
*/
void update(int x,int n){
	t[n].sum++;
	if(t[n].a == x && t[n].b == x){
		return;
	}
	int mid = (t[n].a + t[n].b)/2;
	if(x > mid){
		update(x,2*n+1);
	}else{
		update(x,2*n);
	}
}
int main(){
	int n,m,a[M];
	while(~scanf("%d",&n)){
		make(0,n-1,1);
		int total = 0;
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
			total += query(a[i],n-1,1);
			update(a[i],1);
		}
		int ans = total;
		for(int i=0;i<n;i++){
			total = total - a[i] + (n - a[i] - 1);
			ans = min(ans,total);
		}
		printf("%d\n",ans);
	}
	return 0;
}
/*
total = total - a[i] + (n - a[i] - 1);
比如1 3 6 9 0 8 5 7 4 2
比1小的数有0个,后面比1大的数有8个,1放到后面,少了1个逆序数,又多了8个逆序数
比3小的数有3个,后面比3大的数有6个,3放到后面,少了3个逆序数,又多了6个逆序数
。。。

*/

  

时间: 2024-08-02 02:49:30

Minimum Inversion Number 【线段数】的相关文章

Minimum Inversion Number(线段树单点更新+逆序数)

Minimum Inversion Number(线段树单点更新+逆序数) Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy

hdu 1394 Minimum Inversion Number 线段树 点更新

// hdu 1394 Minimum Inversion Number 线段树 点更新 // // 典型线段树的单点更新 // // 对于求逆序数,刚开始还真的是很年轻啊,裸的按照冒泡排序 // 求出最初始的逆序数,然后按照公式递推,结果就呵呵了 // // 发现大牛都是用线段树和树状数组之类的做的,而自己又在学 // 线段树,所以就敲了线段树. // // 线段树的节点保存一段区间( L,R )内0,1...n一共出现了多少个. // 因为每个数是0,1,2...n-1且没有重复的数字. /

HDU 1394 Minimum Inversion Number.(线段树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 ~~~~ 早起一发线段树,开心又快乐.这题暴力也能水过,同时线段树的效率也就体现的尤为明显了,看了大牛的博客,说是还可以用树状数组,点树和合并序列写,现在还不懂,留着以后在写吧. ~~~~ 大致题意:给定一个数字序列,同时由此可以得到n个序列, 要求从n个序列中找到逆序数最小的序列,输出最小逆序数. 首先介绍下逆序数的概念: 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面

HDU1394 Minimum Inversion Number 线段树+数学

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

Hdu 1394 Minimum Inversion Number(线段树或树状数组)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11981    Accepted Submission(s): 7321 Problem Description The inversion number of a given number sequence a1, a2, ..., a

HDU 1394 Minimum Inversion Number (线段树,单点更新)

C - Minimum Inversion Number Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1394 Appoint description: System Crawler (2015-08-17) Description The inversio

hdu1394 Minimum Inversion Number(线段树单点更新||暴力)

题目链接: huangjing 这个题目暴力和线段树都可以过,但是都需要掌握一个规律.. 当队首元素移到队尾后,可定会减少a[i]个逆序对,然后增加n-1-a[i]个逆序对. 你看比如1移到队尾,那么1>0这个逆序对就会减少,2>1,3>1,4>1这些逆序对就会增加.. 所以发现这个规律就好做了.. 暴力做法就是直接那样模拟.. 线段树做法是首先建立一颗空树,然后插入之前询问这颗树中在[插入元素,n]之间的树,求出来的就是逆序对的个数.然后将其更新进去即可.. 题目: 寻人启事:2

HDU 1394 Minimum Inversion Number (线段树,暴力)

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 seq

hdu 1394 Minimum Inversion Number 线段树

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

hdu - 1394 Minimum Inversion Number(线段树水题)

http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum就是当前数的逆序数. 这样查询完之后得到初始数列的逆序数,要求得所有序列的最小逆序数,还需要循环一次. 设初始序列abcde中逆序数为k,小于a的个数是t-1那么大于a的个数就是n-t,当把a左移一位,原来比a大的都变成了a的逆序对,即逆序数增加了n-t,但是原来比a小的数都变成了顺序, 因此逆序数