Ultra-QuickSort poj2299(树状数组+离散化)

Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 80376 Accepted: 30098

Description

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.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0

题目大意:给我们一个长度为n的序列,采用某种快速排序(交换排序),问最小交换次数。
思路:一天下来就搞了两道题。。。唉。。。多半是废了。
首先因为是交换排序很容易想到用冒泡排序去模拟,果断mle了,这时需要考虑更高效的算法
最小交换次数就是这个序列的逆序数,这个百度有严格证明,我就不多说了,关键是怎么求出这个逆序数。有三种方法可以求逆序数,分别是归并排序,线段树(后续补上),今天写的是树状数组。
由于数据范围非常大,所以需要离散化一下。
然后依次入读每个点再询问,因为第i个点最有有i-1个逆序数,因为树状数组要计数,每次+1,所以我们假设第i个点有i个逆序数(多一个本身,这是我们自己抽象出来的,方便计算),然后第i个数的最多逆序数减去不是不是逆序数的数量,剩下的就是逆序数的数量,然后累加求和就行了。然后我们用树状数组sum[i]维护第i个数不是逆序数的数量,只有是在它之前出现的都不可能是它的逆序数,那么它不是逆序数++。
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=1e6+10;
typedef long long ll;
ll a[maxn],b[maxn];
ll sum[maxn];
ll k;
ll query(ll x){
	ll ans=0;
	for(;x;x-=(x&-x)){
		ans+=sum[x];
	}
	return ans;
}
void add(ll x,ll v){
	for(;x<=k;x+=(x&-x)){
		sum[x]+=v;
	}
}
/* 5 9 1 0 5 4 3 1 2 3 */
int main(){
	ll n;
	while(scanf("%lld",&n)&&n){
		memset(sum,0,sizeof(sum));
		for(ll i=0;i<n;i++){
			scanf("%lld",&a[i]);
			b[i]=a[i];
		}
		sort(b,b+n);
		k=unique(b,b+n)-b;
		ll ans=0;
		for(ll i=0;i<n;i++){
			ll x=lower_bound(b,b+k,a[i])-b;
			x++;
			add(x,1);
			ans+=(i+1-query(x));
		}
		printf("%lld\n",ans);
	}
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# AI面会问哪些问题? #
24960次浏览 492人参与
# 中国电信笔试 #
31122次浏览 283人参与
# 米连集团26产品管培生项目 #
12960次浏览 285人参与
# 你的实习产出是真实的还是包装的? #
18854次浏览 330人参与
# 如果秋招能重来,我会____ #
96710次浏览 500人参与
# 春招至今,你的战绩如何? #
60153次浏览 546人参与
# 开放七大实习专项,百度暑期实习值得冲吗 #
14185次浏览 209人参与
# i人适合做什么工作 #
36934次浏览 124人参与
# 我是面试官,请用一句话让我破防 #
79529次浏览 219人参与
# 哪些公司真双非友好? #
69218次浏览 287人参与
# 金三银四,你的春招进行到哪个阶段了? #
21574次浏览 277人参与
# 找AI工作可以去哪些公司? #
7738次浏览 188人参与
# 从事AI岗需要掌握哪些技术栈? #
7730次浏览 251人参与
# 投递几十家公司,到现在0offer,大家都一样吗 #
339952次浏览 2165人参与
# 面试尴尬现场 #
220775次浏览 861人参与
# 五一之后,实习真的很难找吗? #
102811次浏览 584人参与
# 你做过最难的笔试是哪家公司 #
30318次浏览 193人参与
# 你小时候最想从事什么职业 #
159844次浏览 2072人参与
# 应届生第一份工资要多少合适 #
20489次浏览 84人参与
# 阿里笔试 #
176531次浏览 1302人参与
# 一张图晒出你司的标语 #
3843次浏览 72人参与
# 面试被问期望薪资时该如何回答 #
382478次浏览 2163人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务