Codeforces Round #251(Div. 2) 439D. Devu and his Brother 枚举

D. Devu and his Brother
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays a and bby their father. The array a is given to Devu and b to his brother.

As Devu is really a naughty kid, he wants the minimum value of his array a should be at least as much as the maximum value of his brother's array b.

Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation on any index of the array multiple times.

You need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting.

Input

The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105). The second line will contain n space-separated integers representing content of the array a (1 ≤ ai ≤ 109). The third line will contain m space-separated integers representing content of the array b (1 ≤ bi ≤ 109).

Output

You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.

Examples
input
2 2
2 3
3 5
output
3
input
3 2
1 2 3
3 4
output
4
input
3 2
4 5 6
1 2
output
0
Note

In example 1, you can increase a1 by 1 and decrease b2 by 1 and then again decrease b2 by 1. Now array a will be [3; 3] and array bwill also be [3; 3]. Here minimum element of a is at least as large as maximum element of b. So minimum number of operations needed to satisfy Devu's condition are 3.

In example 3, you don't need to do any operation, Devu's condition is already satisfied.

题意:

给出数组a,b,使得a的最小值比b的最大值小的尽量少,即把a的最小值调整到和b的最大值一样大,如果a的最小值本身就比b的最大值要大,则无需调整。调整的代价为每个数字改动的量。

题解:

如果a的最小值不小于b的最大值,则输出0。

因为到最后肯定是调整到某个值x,使得a的最小值和b的最大值都是x,且这个x一定会是某个ai或者bi,所以我们枚举这个x值,其实就是枚举两个数组里的值,对于枚举的值x,求出把a的最小值调整到x需要的代价up[x],同理再反向扫一遍,求出把b的最大值调整到x的代价down[x],最后扫一遍更新答案。

#include <stdio.h>
#include <algorithm>
using namespace std;
typedef __int64 LL;

const int maxn=100000+10;
int a[maxn],b[maxn];
LL up[maxn*2],down[maxn*2];

int max(int x,int y){
	return x>y?x:y;
}

int min(int x,int y){
	return x<y?x:y;
}

LL Min(LL x,LL y){
	return x<y?x:y;
}

struct pp{
	int v,id;
}c[maxn*2];

int cmp(const pp &x,const pp &y){
	return x.v<y.v;
}

int main()
{
	int i,j,n,m;
	while(scanf("%d%d",&n,&m)!=EOF){
		int x=1e9+7,y=0,tot=0;
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
			x=min(x,a[i]);
			c[++tot].v=a[i];
			c[tot].id=0;
		}
		for(i=1;i<=m;i++){
			scanf("%d",&b[i]);
			y=max(y,b[i]);
			c[++tot].v=b[i];
			c[tot].id=1;
		}
		if(x>=y){
			puts("0");continue;
		}
		sort(c+1,c+tot+1,cmp);     //将两者的值放在一起
		up[1]=0;
		int last=c[1].v,cnt=0;
		if(c[1].id==0) cnt++;    //cnt记录的是当前处理过去了cnt个a[]数组里的数
		for(i=2;i<=tot;i++){
			up[i]=up[i-1]+(LL)cnt*(c[i].v-last);   //这里用的类似差分的思想
			last=c[i].v;
			if(c[i].id==0) cnt++;
		}
		down[tot]=0;last=c[tot].v;cnt=0;
		if(c[tot].id) cnt++;    //cnt记录的是当前处理过去了cnt个b[]数组里的数
		for(i=tot-1;i>=1;i--){
			down[i]=down[i+1]+(LL)cnt*(last-c[i].v);
			last=c[i].v;
			if(c[i].id) cnt++;
		}
		LL res=1e18;
		for(i=1;i<=tot;i++)
			res=Min(res,up[i]+down[i]);
		printf("%I64d\n",res);
	}
	return 0;
}


你可能感兴趣的:(codeforces)