CF 439D(251D题)Devu and his Brother
来源:互联网 发布:程序员简历下载 编辑:程序博客网 时间:2024/05/17 05:08
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 arraysa and b by their father. The arraya 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 arrayb.
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.
The first line contains two space-separated integers n,m (1 ≤ n, m ≤ 105). The second line will containn space-separated integers representing content of the arraya (1 ≤ ai ≤ 109). The third line will containm space-separated integers representing content of the arrayb (1 ≤ bi ≤ 109).
You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.
2 22 33 5
3
3 21 2 33 4
4
3 24 5 61 2
0
In example 1, you can increase a1 by 1 and decreaseb2 by 1 and then again decrease b2 by1. Now array a will be [3;3] and array b will 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 are3.
In example 3, you don't need to do any operation, Devu's condition is already satisfied.
贪心问题,代码不长,好看懂
AC代码如下:
#include<iostream>#include<string>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<map>#include<queue>#include<stack>#include<iomanip>using namespace std;#define ll long long#define M 100005bool cmp(int a,int b){ return a>b;}int min(int a,int b){ return a<b?a:b;}int main(){ int n,m,i; int a[M],b[M]; cin>>n>>m; for(i=0;i<n;i++) cin>>a[i]; for(i=0;i<m;i++) cin>>b[i]; sort(a,a+n); sort(b,b+m,cmp); n=min(n,m); ll sum=0; for(i=0;i<n;i++) { if(b[i]>a[i]) sum+=(ll)(b[i]-a[i]); } cout<<sum<<endl; return 0;}
- CF 439D Devu and his Brother
- CF 439D(251D题)Devu and his Brother
- 439 D. Devu and his Brother
- Codeforces 439D Devu and his Brother
- Codeforces 439D Devu and his Brother
- CodeForces 439D Devu and his Brother
- Codeforces Round #251 (Div. 2) D. Devu and his Brother
- Codeforces Round#251(Div 2)D Devu and his Brother
- Codeforces Round #251 (Div. 2) D Devu and his Brother
- Codeforces Round #251 (Div. 2)-D. Devu and his Brother
- Codeforces 439D Devu and his Brother 三分
- Codeforces 439D Devu and his Brother(排序)
- Codeforces 439D Devu and his Brother【思维+三分查找】
- Codeforecs 439D Devu and his Brother 三分
- Codeforces Round #251 Div2D 439D Devu and his Brother 二分查找STL
- Codeforces Round #251 (Div. 2) 439D Devu and his Brother(脑洞)
- Codeforces Round #251(Div. 2) 439D. Devu and his Brother 枚举
- CF 66D Petya and His Friends
- 小细节:关于C++各种变量的size问题。
- MySQL基本操作命令
- 【android自定义控件】LinearLayout定义ActionBar样式
- vector
- memcached window版服务器端安装
- CF 439D(251D题)Devu and his Brother
- Unix高级编程 使用setvbuf实现setbuf
- C文件流操作的fread和fseek
- Texture类学习
- Linux性能分析2
- android最全的面试题
- 栈的实现:链式栈
- FZU2168 防守阵地 I
- thrift之TTransport类体系原理及源码详细解析1-类结构和抽象基类