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): 17260    Accepted Submission(s): 10496


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

题意:给出一个数组,可以将数组的第一个数放到数组的最后一个数的后面,求经过此种操作后形成数组的最小逆序数。

思路:构建一棵线段树,大区间为[0,n-1],按照数组的顺序插入各个数,按照样例,第一个插入的为1,则将[1,1]置为1,然后向上跟新,然后查询[2,9]区间内已经插入的数的个数,(即比1大且在数组中顺序在1之前的数,1的逆序数),以此类推。

当得到一种数组的逆序数后,可以通过公式 ans = ans + (n - 1 - a[i]) + a[i]来求得最小逆序数。

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int a[5005];
struct node
{
	int l,r,sum;
}t[20020];
int ss;
void build_tree(int l, int r, int k)
{
	if(l == r)
	{
		t[k].l = l;
		t[k].r = r;
		t[k].sum = 0;
		return;
	}
	int mid = (l + r) / 2;
	t[k].l = l;
	t[k].r = r;
	t[k].sum = 0;
	build_tree(l, mid, 2 * k);
	build_tree(mid + 1, r, 2 * k + 1);
}
void insert_tree(int i, int num, int k)
{
	if(t[k].l == t[k].r && t[k].l == i)
	{
		t[k].sum = num;
		return ;
	}
	int mid = (t[k].l + t[k].r) / 2;
	if(i <= mid)insert_tree(i, num, 2 * k);
	else insert_tree(i, num, 2 * k + 1);
	t[k].sum = t[2 * k].sum + t[2 * k + 1].sum;
}
void search_tree(int l, int r, int k)
{
	if(t[k].l == l && t[k].r == r)
	{
		ss += t[k].sum;
		return ;
	}
	int mid = (t[k].l + t[k].r) / 2;
	if(r <= mid) search_tree(l, r, 2 * k);
	else if(l > mid) search_tree(l, r, 2 * k + 1);
	else
	{
 		search_tree(l, mid, 2 * k);
 		search_tree(mid + 1, r, 2 * k + 1);
	}
}
int main()
{
	int n, i, j, ans;
	while(scanf("%d", &n) != EOF)
	{
		ans = 0;
		build_tree(0,n-1,1);
		
		for(i = 0; i < n; i++)
		{
			scanf("%d", &a[i]);
			insert_tree(a[i], 1, 1);//每个数插入的值都为1 
			ss = 0;
			if(a[i] != n - 1)//最大的数的逆序数必为0 
			search_tree(a[i] + 1, n - 1, 1);
			ans += ss;//加上每个数的逆序数 
		}
		int minn = ans;
		for(i = 0; i < n; i++)
		{
			ans = ans + (n - 1 - a[i]) - a[i];
			if(ans < minn)
			minn = ans;
		}
		cout << minn <<endl;
	}
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值