【树状数组+离散化】【逆序对】 POJ 2299 Ultra-QuickSort

题目链接

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 

9 1 0 5 4 ,

Ultra-QuickSort produces the output 

0 1 4 5 9 .

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

 

Description:

交换任意相邻两个数,使得序列变为升序

 

Solution: 

这是一个求解逆序对的问题,首先考虑到利用树状数组来求解。

对于一个给定n个元素的排列 a0, a1, ……an,如果想知道以他为首的逆序对的数目(形如(ai, aj)这样的数对),就是需要计算ai+1, ……, an 这个序列中小于 ai 的元素个数。由于树状数组维护的是前缀和,所以我们可以将 ai 本身作为树状数组的下标,那么每插入一个数,我们只需要对当前 (ai - 1) 计算前缀和,就可以知道 ai 后面有多少个小与 ai 的数了,需要注意的是要从后往前枚举,因为是要寻找 ai 后面有多少个小与 ai 的元素,当枚举到 ai 时,那么我们就可以通过 ans += getSum(ai - 1) 得到前面小于 ai 数目,然后再通过 add(ai, 1) 把 ai 这个数添加到树上。

通常树状数组求逆序数,是将数组元素本身作为树状数组的下标,这就对元素的大小产生了限制,而本题的元素最大可达 999,999,999,所以传统做法明显不可行,这时就需要 离散化 思想了:

题目中的元素都是独一无二的,且最多只有 500000 个,把原始的数映射为 1 ~ n 一共n个数,用一个 f 数组存储离散后的值,f[i] 表示原序列中第 i 个数在将序列排序后所在的位置,之后就可以利用传统做法去求解了。

总的时间复杂度O(nlogn)。

 

Code:

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define fi first
#define se second
#define Fio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fopen freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#define _rush() int T; cin >> T; while(T--)
#define rush() int T; scanf("%d", &T); while(T--)
#define mst(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int Mod = 1e9 + 7;
const int MaxN = 5e5 + 5;

struct node {
	int x, id;
} a[MaxN];

int n;
int presum[MaxN], f[MaxN];

bool cmp(node p, node q) { return p.x < q.x; }

int lowbit(int x) { return x & (-x); }

int add(int p) {
	for(int i = p; i <= n; i += lowbit(i)) presum[i]++;
}

int get_sum(int p) {
	int ans = 0;
	for(int i = p; i > 0; i -= lowbit(i)) ans += presum[i];
	return ans;
}

int main()
{
    Fio;
    
    while(cin >> n) {
    	if(n == 0) return 0;
    	mst(presum, 0);
		for(int i = 1; i <= n; i++) {
			cin >> a[i].x;
			a[i].id = i;
		}
		sort(a + 1, a + 1 + n, cmp);
		LL ans = 0LL;
		for(int i = 1; i <= n; i++) f[a[i].id] = i; 离散化
		for(int i = 1; i <= n; i++) {
			add(f[i]);
			ans += i - get_sum(f[i]); //get_sum实际求的是正序对的数量,用总数对的数量减去正序对的数量就是逆序对的数量了
		}
		cout << ans << endl;
	}
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值