HDU 6318 逆序数

http://acm.hdu.edu.cn/showproblem.php?pid=6318

Long long ago, there was an integer sequence a.
Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will have to pay x yuan for every inversion in the sequence.
You don't want to pay too much, so you can try to play some tricks before he sees this sequence. You can pay y yuan to swap any two adjacent elements.
What is the minimum amount of money you need to spend?
The definition of inversion in this problem is pair (i,j)(i,j) which 1≤i<j≤n1≤i<j≤n and ai>ajai>aj.

Input

There are multiple test cases, please read till the end of input file.
For each test, in the first line, three integers, n,x,y, n represents the length of the sequence.
In the second line, n integers separated by spaces, representing the orginal sequence a.
1≤n,x,y≤1000001≤n,x,y≤100000, numbers in the sequence are in [−109,109][−109,109]. There're 10 test cases.

Output

For every test case, a single integer representing minimum money to pay.

Sample Input

3 233 666
1 2 3
3 1 666
3 2 1

Sample Output

0
3

题目大意:求逆序数,自己交换两个相邻元素需要y元,给另外一个人交换需要x元,问需要的最少钱数。

思路:就是求逆序数,交换两个相邻元素最多让逆序对减1。(很简单 就不证明了吧 自己画一下都懂了)那贪心取x、y最小值和逆序对数量乘起来就完事了。

归并排序:

扫描二维码关注公众号,回复: 5512351 查看本文章
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

const int maxn=100005;
int a[maxn];
int L[maxn];
int R[maxn];

ll Merge(int l,int mid,int r)
{
	int temp1=mid-l;
	int temp2=r-mid;
	for(int i=0;i<temp1;i++)
		L[i]=a[l+i];
	for(int i=0;i<temp2;i++)
		R[i]=a[mid+i];
	int i=0,j=0,k=l;
	ll cnt=0;
	while(i<temp1&&j<temp2)
	{
		if(L[i]>R[j])
		{
			a[k++]=R[j++];
			cnt+=temp1-i;
		}
		else
			a[k++]=L[i++];
	}
	while(i<temp1)
		a[k++]=L[i++];
	while(j<temp2)
		a[k++]=R[j++];
	return cnt;
}

ll MergeSort(int l,int r)
{
	if(l+1<r)
	{
		int mid=(l+r)>>1;
		ll sum1=MergeSort(l,mid);
		ll sum2=MergeSort(mid,r);
		ll sum3=Merge(l,mid,r);
		return sum1+sum2+sum3;
	}
	return 0;
}

int main()
{
	int n,x,y;
	while(~scanf("%d %d %d",&n,&x,&y))
	{
		for(int i=0;i<n;i++)
			scanf("%d",&a[i]);
		printf("%lld\n",MergeSort(0,n)*min(x,y));
	}
	return 0;
}

离散化+树状数组:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

const int maxn=100005;

int a[maxn];
int b[maxn];
int tree[maxn];
int n,x,y;

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

void add(int i)
{
	for(;i<=n;i+=lowbit(i))
		tree[i]+=1;
}

int sum(int i)
{
	int s=0;
	for(;i;i-=lowbit(i))
		s+=tree[i];
	return s;
}

int main()
{
	while(~scanf("%d %d %d",&n,&x,&y))
	{
		memset(tree,0,sizeof(tree));
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			b[i]=a[i];
		}
		sort(b,b+n);
		for(int i=0;i<n;i++)
			a[i]=upper_bound(b,b+n,a[i])-b;
		ll cnt=0;
		for(int i=0;i<n;i++)
		{
			cnt+=i-sum(a[i]);
			add(a[i]);
		}
		printf("%lld\n",cnt*min(x,y));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/88367624
今日推荐