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 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

题意

现在有一个数组,含有n个数(为0-(n-1)的一个排列),依次把第一个数,放到数组最后面,求其中逆序数数目最小是多少?


题解

  • 先求初始状态的逆序数
    我们先建立一颗空树,然后一个一个将节点插入树中,把数组中数的大小看成在树中的位置,然后每插入一个节点,就统计大于这个数的数目,即产生的逆序数的数目,再更新该位置的数值为1.这样就求出了初始状态下的逆序数sum了。

  • 然后要求出最小的逆序数
    因为数组是0到 n-1 的一个排列,所以如果当你把第一个数从第一个放到最后一个的时候,逆序数就会减少 a[i] 个,同时增加 n - (a[i]+1) 个。所以我们只需要枚举依次把第一个数放到最后位置,所产生的逆序数 sum - a[i] + n - a[i] - 1 取最小值即可。


AC-Code

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <fstream>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int INF = 0x7fffffff;
const int MOD = 1e4;
const int MAXN = 5e3 + 7;

struct node {
	int left;
	int right;
	int data;
}tree[MAXN << 2];
int n;
void PushUp(int rt) {
	tree[rt].data = tree[rt << 1].data + tree[rt << 1 | 1].data;
}

void build(int rt, int l, int r) {
	tree[rt].left = l;
	tree[rt].right = r;
	tree[rt].data = 0;
	if (l == r) {
		return;
	}
	int mid = (l + r) >> 1;
	build(rt << 1, l, mid);
	build(rt << 1 | 1, mid + 1, r);
	PushUp(rt);
}
void updata(int rt, int id) {
	if (tree[rt].left == tree[rt].right) {
		tree[rt].data++;
		return;
	}
	int mid = (tree[rt].left + tree[rt].right) >> 1;
	if (id <= mid)	updata(rt << 1, id);
	if (id > mid)	updata(rt << 1 | 1, id);
	PushUp(rt);
}
int Query(int rt, int l, int r) {
	if (l <= tree[rt].left && tree[rt].right <= r) {
		return tree[rt].data;
	}
	int ans = 0;
	int mid = (tree[rt].left + tree[rt].right) >> 1;
	if (l <= mid)	ans += Query(rt << 1, l, r);
	if (r > mid)	ans += Query(rt << 1 | 1, l, r);
	return ans;
}
int a[MAXN];
int main() {
	ios;
	while (cin >> n) {
		build(1, 1, n);
		int sum = 0;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
			sum += Query(1, a[i] + 1, n);
			updata(1, a[i] + 1);
		}
		int ans = sum;
		for (int i = 1; i <= n; i++) {
			sum = sum - a[i] + n - a[i] - 1;
			ans = min(ans, sum);
		}
		cout << ans << endl;
	}
	return 0;
}
发布了104 篇原创文章 · 获赞 60 · 访问量 5879

猜你喜欢

转载自blog.csdn.net/Q_1849805767/article/details/103143390