codeforces 251 div2 D. Devu and his Brother 三分
1 second
256 megabytes
standard input
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.
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).
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 三分的更多相关文章
- 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 ...
- Codeforces 439D Devu and his Brother 三分
题目链接:点击打开链接 = - =曾经的三分姿势不对竟然没有被卡掉,,,太逗.. #include<iostream> #include<string> #include< ...
- 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 ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
随机推荐
- KMP(http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2772)
#include <stdio.h>#include <string.h>#include <stdlib.h>char a[1000001],b[1000001] ...
- PAT 1064 Complete Binary Search Tree[二叉树][难]
1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a b ...
- C# 序列化(Serialize)与反序列化(Deserialize)
序列化是将对象的状态信息转换为可保持或传输的格式的过程(一堆字符),比如转化为二进制.xml.json等的过程. 反序列化就是将在序列化过程中所生成的二进制串.xml.json等转换成数据结构或者对象 ...
- eigen 笔记1
c++ 的 eigen 类似于 python 的 numpy, 还有一个类似的库是 Armadillo, 当然还有 opencv. Armadillo 与 matlab 在函数名称上更接近, 但是 T ...
- jQuery 批量操作checkbox
困扰很久的问题: 如果只是 $('input[type=checkbox]').attr('checked',true);//全选 $('input[type=checkbox]').attr('ch ...
- 论文笔记:语音情感识别(五)语音特征集之eGeMAPS,ComParE,09IS,BoAW
一:LLDs特征和HSFs特征 (1)首先区分一下frame和utterance,frame就是一帧语音.utterance是一段语音,是比帧高一级的语音单位,通常指一句话,一个语音样本.uttera ...
- 绘制loss曲线
第一步保存日志文件,用重定向即可: $TOOLS/caffe train --solver=$SOLVERFILE >& |tee out.log 第二步直接绘制: python plo ...
- Ubuntu系统下在github中新增库的方法
上一篇介绍了Ubuntu16.04系统下安装git的方法.本博客介绍怎么在github上怎么新建库. 如图 root@ranxf:/home/ranxf/learnGit/ranran_jiekou# ...
- iphone6 inline-flex兼容问题
在编写微信端页面时,遇到这样的问题:position属性为flex的导航栏,其li标签在其余设备上显示正常,但在iphone6上浮动错误. 究其原因,是iphone6不支持position属性中的fl ...
- 什么是公网IP、内网IP和NAT转换?
搞网络通信应用开发的程序员,可能会经常听到外网IP(即互联网IP地址)和内网IP(即局域网IP地址),但他们的区别是什么? 1.引言 搞网络通信应用开发的程序员,可能会经常听到外网IP(即互联网IP地 ...