1. 程式人生 > >Codeforces Round #251(Div. 2) 439D. Devu and his Brother 列舉

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;
}