给定a数组和b数组,两数组的元素每次可以增减1,要求增减最少使得a数组中最小数大于等于b数组最大数。
把a,b数组都存到c数组中,c[m]即为最终那个数,然后一个一个加起来就是答案。
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int maxn = 1e5 + 5;
typedef long long ll;
int main(int argc, char const *argv[])
{
int n, m;
while(scanf("%d%d", &n, &m) != EOF) {
ll a[maxn], b[maxn], c[maxn * 2];
for(int i = 0; i < n; ++i) {
scanf("%lld", &a[i]);
c[i] = a[i];
}
for(int j = 0; j < m; ++j) {
scanf("%lld", &b[j]);
c[j + n] = b[j];
}
sort(c, c + m + n);
ll ans = 0, t = c[m];
for(int i = 0; i < n; ++i)
ans += max((ll)0, t - a[i]);
for(int i = 0; i < m; ++i)
ans += max((ll)0, b[i] - t);
printf("%lld\n", ans);
}
return 0;
}