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 nm (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.

题意:两个序列,每次只能加一减一,使得第一个序列的最小值要大于等于第二个序列的最大值,求最少的操作次数;

思路:一个凹性函数,利用三分求答案;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
ll a[];
ll b[];
ll check(ll x,ll y,ll cha)
{
ll sum=;
for(ll i=;i<x;i++)
if(cha>a[i])
sum+=(cha-a[i]);
for(ll i=;i<y;i++)
if(cha<b[i])
sum+=b[i]-cha;
return sum;
}
int main()
{
ll x,y,z,i,t;
scanf("%I64d%I64d",&x,&y);
ll maxx=;
ll minn=;
for(i=;i<x;i++)
{
scanf("%I64d",&a[i]);
minn=min(minn,a[i]);
}
for(i=;i<y;i++)
{
scanf("%I64d",&b[i]);
maxx=max(maxx,b[i]);
}
if(minn>=maxx)
{
printf("0\n");
return ;
}
ll st=minn;
ll en=maxx;
ll mid,midd;
ll ans=min(check(x,y,minn),check(x,y,maxx));
while(st<en)
{
mid=(st+en)/;
midd=(mid+en)/;
ll checkmid=check(x,y,mid);
ll checkmidd=check(x,y,midd);
if(checkmid>checkmidd)
st=mid;
else
en=midd;
ans=min(ans,min(checkmid,checkmidd));
}
printf("%I64d\n",ans);
return ;
}

codeforces 251 div2 D. Devu and his Brother 三分的更多相关文章

  1. codeforces 251 div2 C. Devu and Partitioning of the Array 模拟

    C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...

  2. Codeforces 439D Devu and his Brother 三分

    题目链接:点击打开链接 = - =曾经的三分姿势不对竟然没有被卡掉,,,太逗.. #include<iostream> #include<string> #include< ...

  3. CF 439D(251D题)Devu and his Brother

    Devu and his Brother time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  5. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  6. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  7. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  8. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  9. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

随机推荐

  1. springboot中的日志配置

    日志方式:每天日志存放在一个文件中,info和warn日志存放一个文件,error存放一个文件 创建文件 logback-spring.xml <?xml version="1.0&q ...

  2. spring-data-mongodb关于id的坑

    有如下Mongo记录: 在Java中对应2个类来表示此结构: public class SG_IMAGERELATION { @Id private String id; private int gi ...

  3. python图片处理(二)

    未经允许,请勿转载!!!! 这次打算先写处理图片的方法,然后再调用方法来运行 下面先写的是处理图片的方法: # -*- coding: utf-8 -*- import os import matpl ...

  4. Matlab中图像处理实例:灰度变换,空域滤波,频域滤波,傅里叶变换的实现

    http://blog.sciencenet.cn/blog-95484-803140.html % %图像灰度变换 % f = imread('E:\2013第一学期课程\媒体计算\实验一\Img\ ...

  5. 关于DOM2级事件的事件捕获和事件冒泡

    DOM2级事件中addEventListener的执行机制,多个addEventListener同时添加时的执行先后规律: W3C的DOM事件触发分为三个阶段:①.事件捕获阶段,即由最顶层元素(一般是 ...

  6. 20165207 Exp1 PC平台逆向破解

    20165207 Exp1 PC平台逆向破解 0.写在最前面 在做三个实验的前两个的时候,我还没有到博客里去看作业的要求.当时我的主机名是kali5207也就是用我的学号命名的,要求的是姓名全拼命名k ...

  7. Linux基础命令---znew

    znew 将compress压缩成的”.Z”文件,转换成“.gz”格式的文件.ZNew将文件从.z(压缩)格式重新压缩到.gz(Gzip)格式.如果要重新压缩已以gzip格式的文件,请重命名该文件以强 ...

  8. Linux基础命令---rmdir

    rmdir 删除一个空目录,可以同时删除途经的父目录,但是要确保父目录中没有其他内容. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. ...

  9. 谷歌发布"自动机器学习"技术 AI可自我创造

    谷歌发布"自动机器学习"技术 AI可自我创造 据Inverse报道,今年5月份,谷歌宣布其人工智能(AI)研究取得重大进展,似乎帮助科幻小说中最耸人听闻的末日预言成为现实.谷歌推出 ...

  10. 通过Jenkins + Docker实现antdPro自动化推送私服、自动容器化部署功能

    Docker与Docker私服 1. 安装docker https://docs.docker.com/install/ 2. 配置docker镜像加速 https://www.daocloud.io ...