Wannafly挑战赛16 AB序列

链接: https://www.nowcoder.com/acm/contest/113/B
来源:牛客网

给长度为n的序列A,长度为m的序列B。可以给A序列里每个元素加上x且B序列里每个元素减去x (x可以是负数),问

的最小值。

样例:

4 5
-8 2 -4 10

5 -5 -4 -9 10

输出:

57

被自己菜醒的式子变化:   == ∑|(x-(-Ai))| + ∑|(x-Bi)|+ | x - 0 |.

是该对自己的菜警告一下!

#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
const int maxn = 100000;
const int inf = (1e9+1005);
int n,m,a[maxn*2+5];
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]*=-1;
    for(int i=1;i<=m;i++)
    {
        int x;
        scanf("%d",&x);
        a[n+i] = x;
    }
    a[n+m+1] = 0;
    sort(a+1,a+n+m+2);
    int inx = (n+m+1)/2 + (n+m+1)%2;
    ll ans = 0;
    for(int i=1;i<=n+m+1;i++) ans+=abs(a[inx]-a[i]);
    printf("%lld\n",ans);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_32944513/article/details/80516195
今日推荐