poj2299 Ultra-QuickSort(线段树计数问题)

Ultra-QuickSort
Time Limit: 7000MS

Memory Limit: 65536K
Total Submissions: 46293

Accepted: 16846

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
分析:

我们先将原数组每个值附上一个序号index,再将它排序。如题目的例子:

num:   9  1  0  5  4

index:  1  2  3  4  5

排序后:

num:   0  1  4  5  9

index:  3  2  5  4  1

然后由于排序后num为0的点排在原来数组的第3个,所以为了将它排到第一个去,那就至少需要向前移动两次,同时它也等价于最小的数0之前有2个数比它大(所以要移动两次),将0移到它自己的位置后,我们将0删掉(目的是为了不对后面产生影响)。再看第二大的数1,它出现在原数组的第二个,他之前有一个数比它大所以需要移动一次。这样一直循环下去那么着5个数所需要移动的次数就是:

num:  0  1  4  5  9

次数      2  1  2  1  0

将次数全部要加起来就是最后所需要移动的总次数。

方法章爷是已经告诉我了,但是最初我一直是觉得不好实现。到后来才慢慢、慢慢弄好。方法就是在建一棵树时,不是直接将原来的num放进树里面,而是将它的下标放进树里面,最初每个节点上赋值为1.然后当查找第一个num时,由于是找的下标为3的位置,所以我们就直接找区间[1,3)之间有多少个1(就是求前导和),这里面1的个数就是第一个num=0索要移动的次数,然后我们把0去掉,其实也就是吧下标为3的那个1去掉。这样每个值就依次计算出来了。



参考代码:
<span style="font-family:KaiTi_GB2312;">/*******************************************************************************
 * Author :          jinbao
 * Email :           dongjinbao913106840144@gmail.com
 * Last modified :   2015-05-04 00:19
 * Filename :        poj2299.cpp
 * Description :     
 * *****************************************************************************/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAX = 500000+5;
int tree[4*MAX];
struct array{
	int index;
	int value;
}a[MAX];
int n;
bool cmp(array x,array y){
	if (x.value<y.value)
		return true;
	return false;
}
void build(int node,int begin,int end){
	if (begin==end)
		tree[node]=1;
	else{
		build(node*2,begin,(begin+end)/2);
		build(node*2+1,(begin+end)/2+1,end);
		tree[node]=tree[node*2]+tree[node*2+1];
	}
}
int query(int node,int begin,int end,int left,int right){
	if (left>right)
		return 0;
	if (end<left || right<begin)
		return 0;
	if (begin>=left && right>=end)
		return tree[node];
	return query(node*2,begin,(begin+end)/2,left,right)+query(node*2+1,(begin+end)/2+1,end,left,right);
}
void update(int node,int begin,int end,int index,int value){
	if (begin==end){
		tree[node]=value;
		return;
	}
	int mid=(begin+end)/2;
	if (mid>=index)
		update(node*2,begin,mid,index,value);
	else
		update(node*2+1,mid+1,end,index,value);
	tree[node]=tree[node*2]+tree[node*2+1];
}
int main(){
	while (~scanf("%d",&n)){
		if (n==0)
			break;
		for (int i=1;i<=n;i++){
			scanf("%d",&a[i].value);
			a[i].index=i;
		}
		sort(a+1,a+n+1,cmp);
		build(1,1,n);;
		ll ans=0;
		for (int i=1;i<=n;i++){
			ans+=query(1,1,n,1,a[i].index-1);
			update(1,1,n,a[i].index,0);
		}
		printf("%lld\n",ans);
	}

	return 0;
}
</span>


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值