AB序列 凹函数的性质

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

题目描述

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

输入描述:

第一行两个整数分别表示n,m
接下来一行n个整数表示序列A
接下来一行m个整数表示序列B

输出描述:

输出一个整数表示答案
示例1

输入

复制
4 5
-8 2 -4 10
5 -5 -4 -9 10

输出

复制
57
 
   
  

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 #include <queue>
 6 #include <algorithm>
 7 #include <cstdlib>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 using namespace std;
12 
13 const int maxn = 2e5 + 10;
14 const int mod = 1e9 + 7;
15 typedef long long LL;
16 int a[maxn], n, m, x;
17 int main() {
18     scanf("%d%d", &n, &m);
19     for (int i = 1 ; i <= m + n ; i++) {
20         scanf("%d", &x);
21         if (i <= n) a[i] = x;
22         else a[i] = -x;
23     }
24     sort(a + 1, a + n + m + 2);
25     LL sum = 0;
26     for (int i = 1 ; i <= n + m + 1 ; i++ )
27         sum += (LL)(abs(a[i] - a[(n + m + 2) / 2]));
28     printf("%lld\n", sum);
29     return 0;
30 }

 




转载于:https://www.cnblogs.com/qldabiaoge/p/9092187.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值