`
阿尔萨斯
  • 浏览: 4170549 次
社区版块
存档分类
最新评论

Codeforces 439D Devu and his Brother(排序)

 
阅读更多

题目链接:Codeforces 439D Devu and his Brother


题目大意:Devu和他的哥哥互相深爱着对方,我确信他们是搞基的,为此我还去查了一下Devu是男人名还是女人名,但是后来发现His Brother,所以可以证明,他们就是搞基的。题目很简单,父亲给了他们两个人分别一个数组。但是Devu希望自己最小的数都可以不必哥哥最大的数小,现在对数组中的数有两种操作,一种是加1,一种是减1,问最少进行几次操作可以使得两个数组满足要求。


解题思路:将两个数组合并在一起,然后排序,第m个就是交界值。


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const int N = 1e5+5;

int n, m;
ll a[N], b[N], c[N*2];

inline ll max(ll a, ll b) {
    return a > b ? a : b;
}

int main () {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
        c[i] = a[i];
    }

    for (int i = 0; i < m; i++) {
        scanf("%lld", &b[i]);
        c[i+n] = b[i];
    }

    sort(c, c + n + m);

    ll tmp = c[m], ans = 0;

    for (int i = 0; i < n; i++)
        ans += max(0, tmp - a[i]);

    for (int i = 0; i < m; i++)
        ans += max(0, b[i] - tmp);

    printf("%lld\n", ans);
    return 0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics