Codeforces Round #251 (Div. 2) 439D Devu and his Brother(脑洞)
2015-08-29 14:19
309 查看
D. Devu and his Brother
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays a and bby
their father. The array a is given to Devu and b to
his brother.
As Devu is really a naughty kid, he wants the minimum value of his array a should be at least as much as the maximum value of his brother's
array b.
Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation
on any index of the array multiple times.
You need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting.
Input
The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105).
The second line will contain n space-separated integers representing content of the array a (1 ≤ ai ≤ 109).
The third line will contain m space-separated integers representing content of the array b (1 ≤ bi ≤ 109).
Output
You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.
Sample test(s)
input
output
input
output
input
output
Note
In example 1, you can increase a1 by 1 and
decrease b2 by 1 and
then again decrease b2 by 1.
Now array a will be [3; 3]
and array bwill also be [3; 3].
Here minimum element of a is at least as large as maximum element of b.
So minimum number of operations needed to satisfy Devu's condition are 3.
In example 3, you don't need to do any operation, Devu's condition is already satisfied.
给定a数组和b数组,两数组的元素每次可以增减1,要求增减最少使得a数组中最小数大于等于b数组最大数。
把a,b数组都存到c数组中,c[m]即为最终那个数,然后一个一个加起来就是答案。
AC代码:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays a and bby
their father. The array a is given to Devu and b to
his brother.
As Devu is really a naughty kid, he wants the minimum value of his array a should be at least as much as the maximum value of his brother's
array b.
Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation
on any index of the array multiple times.
You need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting.
Input
The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105).
The second line will contain n space-separated integers representing content of the array a (1 ≤ ai ≤ 109).
The third line will contain m space-separated integers representing content of the array b (1 ≤ bi ≤ 109).
Output
You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.
Sample test(s)
input
2 2 2 3 3 5
output
3
input
3 2
1 2 33 4
output
4
input
3 2 4 5 6 1 2
output
0
Note
In example 1, you can increase a1 by 1 and
decrease b2 by 1 and
then again decrease b2 by 1.
Now array a will be [3; 3]
and array bwill also be [3; 3].
Here minimum element of a is at least as large as maximum element of b.
So minimum number of operations needed to satisfy Devu's condition are 3.
In example 3, you don't need to do any operation, Devu's condition is already satisfied.
给定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; }
相关文章推荐
- WebService注解汇总
- 使用shell操作数据库
- 全局大喇叭 广播机制 Receiver
- Linux命令之文本搜索工具grep、egrep、fgrep
- 腾讯坐标转换百度坐标
- boost编译命令
- material design 的android开源代码整理
- 基于CDH5.4的Spark1.4.1下SparkR的部署
- redhat5.5 x64 安装oracle 11g
- 安装完Git后的相关操作总结
- unity3d游戏开发教程
- Hibernate配置文件详解
- 工作中Linux的使用总结
- sizeof(struct)说明
- LeetCode(242) Valid Anagram
- 图片自动旋转
- pytho 协程 yield
- Redis set 类型 查询命令
- tomacat无法启动---174 javajni.c] [error] %1 不是有效的 Win32 应用程序。
- 【more effective c++读书笔记】【第3章】异常(2)