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

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值