poj 2299 树状数组入门 求逆序数

点击打开链接


Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 63697 Accepted: 23752

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

说白了,求逆序数。

可以用线段树或树状数组或归并排序法写

9 1 0 5 4

1 2 3 4 5

排序后就是

0 1 4 5 9

3 2 5 4 1

然后求0的逆序数,0在之前的数组里下标为3,0目前是最小的数且前面有2个数,那么0的逆序数就是2,算完0的逆序数就删除0;

然后求1的逆序数,1在原数组中下标为2,因为删除了0,1是最小的数,1前面有一个数,1的逆序数为1,再删除1;

然后求4的逆序数,4在原数组下标为5,因为删除了0和1,4前面的数只有2个,因此4的逆序数为2,删除4;

依次类推

思路是这样,那么求值和删除的过程中可用树状数组或线段树维护

树状数组:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
	int num;
	int data;
}a[1000000];
int c[600000];
long long ans=0;
bool cmp(node c,node d)
{
	return c.data<d.data;
}
void insert(int i,int n,int key)
{
	while(i<=n)
	{
		c[i]+=key;
		i+=i&(-i);
	}
}
void getsum(int i)
{
	while(i>0)
	{
		ans+=c[i];
		i-=i&(-i); 
	}
}
int main()
{
	int n;
	while(~scanf("%d",&n)&&n)
	{
		int i;
		memset(c,0,sizeof(c));
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i].data);
			a[i].num=i;
			insert(i,n,1);
		}
		sort(a+1,a+n+1,cmp);
		ans=0;
		for(i=1;i<=n;i++)
		{
			getsum(a[i].num);
			ans--;//之前计算的时候包括了a[i].num本身,因此算出来的结果要-1; 
			insert(a[i].num,n,-1); 
		}
		printf("%lld\n",ans);
	}
}

线段树:

#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
	int num;
	int data;
}a[600000];
int tree[2000000];
long long ans;
bool cmp(node c,node d)
{
	return c.data<d.data;
}
void build(int l,int r,int node)
{
	if(l==r)
	{
		tree[node]=1;
		return;
	}
	int m=(l+r)/2; 
	build(l,m,node*2);
	build(m+1,r,node*2+1);
	tree[node]=tree[node*2]+tree[node*2+1];
}
void query(int l,int r,int node,int ql,int qr)
{
	if(l>=ql&&r<=qr)
	{
		ans+=tree[node];
		return;
	}
	int m=(l+r)/2;
	if(m<qr)
	query(m+1,r,node*2+1,ql,qr);
	if(m>=ql)
	query(l,m,node*2,ql,qr); 	
}
void update(int l,int r,int node,int pos)
{
	if(l==r)
	{
		tree[node]=0;
		return;
	}
	int m=(l+r)/2;
	if(m>=pos)
	update(l,m,node*2,pos);
	else
	update(m+1,r,node*2+1,pos);
	tree[node]=tree[node*2]+tree[node*2+1];
}
int main()
{
	int n;
	while(~scanf("%d",&n)&&n)
	{
		int i;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i].data);
			a[i].num=i;
		}
		sort(a+1,a+n+1,cmp);
		build(1,n,1);
		ans=0;
		for(i=1;i<=n;i++)
		{
			query(1,n,1,1,a[i].num);
			ans--;//之前计算的时候包括了a[i].num本身,因此算出来的结果要-1; 
			update(1,n,1,a[i].num);
		}
		printf("%lld\n",ans);
	}
}

归并排序法:
#include<stdio.h>
int a[1000005],temp[1000005];
long long count; 
void Merge_sort(int L,int mid,int R)
{
	int i=L,j=mid+1,k=L;
	while(i<=mid&&j<=R)
	{
		if(a[i]<=a[j])
		temp[k++]=a[i++];
		else
		{
			temp[k++]=a[j++];
			count+=(mid-i+1);
		}
	}
	while(i<=mid)
	temp[k++]=a[i++];
	while(j<=R)
	temp[k++]=a[j++];
	for(i=L;i<=R;i++)
	a[i]=temp[i];
}
void Merge(int L,int R)
{
	if(L<R)
	{
		int mid=(L+R)/2;
		Merge(L,mid);
		Merge(mid+1,R);
		Merge_sort(L,mid,R);		
	}
}
int main()
{
	int N;
	while(~scanf("%d",&N)&&N)
	{
		int i;
		for(i=0;i<N;i++)
		scanf("%d",&a[i]);
		count=0;
		Merge(0,N-1);
		printf("%lld\n",count);
	}
}

        

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙橘子猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值