题意:给出a数组和b数组,他们的长度最大1e5,元素范围是1到1e9,问你让a数组最小的数比b数组最大的数要大需要的最少改变次数是多少。每次改变可以让一个数加一或减一

分析:枚举a数组和b数组的所有的元素x,作为他们的界限,也就是说a数组所有的数要大于等于x,b数组所有的数要小于等于x,再利用前缀和+二分,分别求出ab数组需要改变的次数,在所有的方案中取一个最小值

代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
ll sum1[maxn],sum2[maxn];
int num1[maxn],num2[maxn];
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&num1[i]);
for(int i=1;i<=m;i++)
scanf("%d",&num2[i]);
sort(num1+1,num1+1+n);
sort(num2+1,num2+1+m);
for(int i=1;i<=n;i++)
sum1[i]=sum1[i-1]+num1[i];
for(int i=1;i<=m;i++)
sum2[i]=sum2[i-1]+num2[i];
ll ans=1e18;
for(int a=1;a<=n+m;a++)
{
int i;
if(a<=n)i=num1[a];
else i=num2[a-n];
ll ans1,ans2;
if(num1[1]>=i)ans1=0;
else
{
int st=1,en=n;
while(st!=en)
{
int md=(st+en)/2;
if(num1[md+1]<=i)st=md+1;//可以修改的下标
else en=md;
}
ans1=(ll)i*st-sum1[st];
}
if(num2[m]<=i)ans2=0;
else
{
int st=1,en=m;
while(st!=en)
{
int md=(st+en)/2;
if(num2[md]>=i)en=md;
else st=md+1;
}
ans2=sum2[m]-sum2[st-1]-(ll)i*(m-st+1);
}
//cout<<i<<" "<<ans1<<" "<<ans2<<endl;
ans=min(ans1+ans2,ans);
}
printf("%lld\n",ans);
return 0;
}

  

codeforces#439 D. Devu and his Brother (二分)的更多相关文章

  1. Codeforces 439 A. Devu, the Singer and Churu, the Joker

    这是本人第一次写代码,难免有点瑕疵还请见谅 A. Devu, the Singer and Churu, the Joker time limit per test 1 second memory l ...

  2. codeforces 439 E. Devu and Birthday Celebration 组合数学 容斥定理

    题意: q个询问,每一个询问给出2个数sum,n 1 <= q <= 10^5, 1 <= n <= sum <= 10^5 对于每一个询问,求满足下列条件的数组的方案数 ...

  3. codeforces 251 div2 D. Devu and his Brother 三分

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

  4. 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 ...

  5. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  6. Codeforces Round#251(Div 2)D Devu and his Brother

    --你以为你以为的.就是你以为的? --有时候还真是 题目链接:http://codeforces.com/contest/439/problem/D 题意大概就是要求第一个数组的最小值要不小于第二个 ...

  7. Codeforces 439D Devu and his Brother 三分

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

  8. Codeforces 749D:Leaving Auction(set+二分)

    http://codeforces.com/contest/749/problem/D 题意:有几个人在拍卖场竞价,一共有n次喊价,有q个询问,每一个询问有一个num,接下来num个人从这次拍卖中除去 ...

  9. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

随机推荐

  1. 使用kubeadm部署Kubernetes集群

    一.环境架构与部署准备 1.集群节点架构与各节点所需安装的服务如下图: 2.安装环境与软件版本: Master: 所需软件:docker-ce 17.03.kubelet1.11.1.kubeadm1 ...

  2. 【转】MVC HtmlHelper用法大全

    HtmlHelper用来在视图中呈现 HTML 控件. 以下列表显示了当前可用的一些 HTML 帮助器. 本主题演示所列出的带有星号 (*) 的帮助器. ActionLink - 链接到操作方法. B ...

  3. Hibernate框架笔记03表操作多对多配置

    目录 1. 数据库表与表之间的关系 1.1 一对多关系 1.2 多对多关系 1.3 一对一关系[了解] 2. Hibernate的一对多关联映射 2.1 创建一个项目,引入相关jar包 2.2. 创建 ...

  4. Elasticsearch系列(3):Elasticsearch操作入门

    创建Index 新建Index,可以直接向Elastic服务器发送PUT请求,比如下面的命令创建了一个名为:logdb的Index. [root@elsearchserver ~]# curl -X ...

  5. ajax请求code:200但是进入error函数

    1.dataType 由json改成text; 2.后台报错了

  6. WebLogic使用总结

    一. WebLogic安装 WebLogic使用总结(一)——WebLogic安装 一.下载WebLogic 到Oracle官网http://www.oracle.com/ 下载WebLogic(根据 ...

  7. windows下QT打包

    1.找到对应的MinGW命令,打开 2.进入exe目录 3.执行windeployqt XX.exe

  8. 第一个 java 程序

    java程序的运行机制 JVM实现了跨平台 JDK > JRE > JVM java Development Kit(JDK)包含:JRE,以及增加编译器和调试器等用于程序开发的文件 Ja ...

  9. Win7 64位下安装64bit MS SQL Server2005时安装不了Reporting Services的处理办法

    警告截图: 解决办法: 在cmd窗口运行如下脚本即可: "cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/A ...

  10. SQLServer之修改视图

    修改视图注意事项 修改先前创建的视图. 其中包括索引视图. ALTER VIEW不影响相关的存储过程或触发器,并且不会更改权限. 如果原来的视图定义是使用 WITH ENCRYPTION 或 CHEC ...