「CF439D」Devu and his Brother
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 b by 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.
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).
You need to output a single integer representing the minimum number of operations needed to satisfy Devu’s condition.
1 2 3 |
2 2 2 3 3 5 |
1 |
3 |
1 2 3 |
3 2 1 2 3 3 4 |
1 |
4 |
1 2 3 |
3 2 4 5 6 1 2 |
1 |
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 b will 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.
题解
一道不难的题不知道为啥被我搞复杂了,似乎英语水平是硬伤
首先ab俩数组一起放进v数组
a升序b降序排序,然后维护前缀和
计算出f1[i]表示a前i项都变为v[i]的代价
f2同理
然后扫一遍得出答案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #define inf 0x7fffffff #define ll long long #define linf 1LL<<50 using namespace std; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,cnt; ll a[100005],b[100005]; ll v[200005]; ll f1[200005],f2[200005]; ll sum,ans; inline bool cmp(int x,int y){return x>y;} int main() { n=read(),m=read(); for(int i=1;i<=n;i++) a[i]=read(),v[++cnt]=a[i]; for(int i=1;i<=m;i++) b[i]=read(),v[++cnt]=b[i]; for(int i=1;i<=200000;i++) f1[i]=f2[i]=linf; sort(a+1,a+n+1); sort(b+1,b+m+1,cmp); int k=1;sum=0; sort(v+1,v+cnt+1); for(int i=1;i<=cnt;i++) { while(a[k]<=v[i]&&k<=n){sum+=a[k];k++;} f1[i]=v[i]*(k-1)-sum; } k=1;sum=0; sort(v+1,v+cnt+1,cmp); for(int i=1;i<=cnt;i++) { while(b[k]>=v[i]&&k<=n){sum+=b[k];k++;} f2[cnt-i+1]=sum-v[i]*(k-1); } ans=linf; for(int i=1;i<=cnt;i++) ans=min(ans,f1[i]+f2[i]); printf("%I64d",ans); return 0; } |