——你以为你以为的。就是你以为的?

——有时候还真是

题目链接:http://codeforces.com/contest/439/problem/D

题意大概就是要求第一个数组的最小值要不小于第二个数组的最大值,你所能做的就是对数组中的某一个数进行+1/-1的操作。最后问操作次数最少须要多少次。

起初这题我看着例子就開始瞎YY了,YY思路如图= =|||

看似有那么一丁点道理,可是事实上是无法这么找到的,非常快就自己找到了反例。。

只是之所以说是有那么一点道理是由于例子中的边缘数正好是它的平衡点,所谓平衡点就是将第一个数组里的数提升到某一个数a之上,把第二个数组的全部数降到数a之下。这就好比将一个天平平衡了。也就是最优解了。

可随之而来的问题也就非常明了了。怎样找到哪个平衡数呢?

我们最好还是先把两个数组里的数存到同一个数组里。然后升序排序。这样我们就得到了一条线性的数列,那么既然是n+m个数里前m个要比后n个小,也就非常自然的能找到平衡点为数列其中的第m或者第m+1个数了。

#include<cstdio>
#include<algorithm>
#define MAX(a,b) ((a>b)?(a):(b))
using namespace std;
int a[1000010],b[1000010],c[2000020];
int main(){
int n,m;
long long ans= 0LL;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++) scanf("%d",&a[i]),c[i]=a[i];
for(int i=0;i<m;i++) scanf("%d",&b[i]),c[n+i]=b[i];
sort(c,c+m+n);
int temp=c[m];
for(int i=0;i<n;i++) ans+=MAX(0,(temp-a[i]));
for(int i=0;i<m;i++) ans+=MAX(0,(b[i]-temp));
printf("%I64d\n",ans);
return 0;
}

Codeforces Round#251(Div 2)D Devu and his Brother的更多相关文章

  1. Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Array

    注意p的边界情况,p为0,或者 p为k 奇数+偶数 = 奇数 奇数+奇数 = 偶数 #include <iostream> #include <vector> #include ...

  2. Codeforces Round #251 (Div. 2) B. Devu, the Dumb Guy

    注意数据范围即可 #include <iostream> #include <vector> #include <algorithm> using namespac ...

  3. Codeforces Round #251 (Div. 2) A - Devu, the Singer and Churu, the Joker

    水题 #include <iostream> #include <vector> #include <algorithm> using namespace std; ...

  4. Codeforces Round 251 (Div. 2)

    layout: post title: Codeforces Round 251 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  5. Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥

    E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...

  6. Codeforces Round #258 (Div. 2)E - Devu and Flowers

    题意:n<20个箱子,每个里面有fi朵颜色相同的花,不同箱子里的花颜色不同,要求取出s朵花,问方案数 题解:假设不考虑箱子的数量限制,隔板法可得方案数是c(s+n-1,n-1),当某个箱子里的数 ...

  7. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  8. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  9. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

随机推荐

  1. CentOS 7下Java的SecureRandom种子初始化失败解决办法

    io.netty.util.internal.ThreadLocalRandom getInitialSeedUniquifierWARNING: Failed to generate a seed ...

  2. jsp中跳出循环

    <c:otherwise> <c:set var="flag" value="true" /><!-- 设置flag --> ...

  3. spring mvc中拦截器配置mvc:interceptors

    其实在mvc:interceptors标签中,有两种类型的配置,一种直接配置一个bean(bean和ref归为一类),另一种还要配置上拦截的路径和排除的路径.直接配置的bean那就代表对所有的请求进行 ...

  4. PHP——smarty模板(第一天)

    smarty.class.php 主要的东西放在类里面templates 放模板templates_c 放缓存 类里面 $smarty->assign("author",&q ...

  5. Ubuntu安装新版本nodejs的5种姿势

    引言: 写这篇文章之前,关于ubuntu14.04(Trusty)默认安装的NodeJS版本是0.10.25百思不解(什么鬼,哪一年的NodeJS) 写这篇文章之时,NodeJS的LTS版本号都已经1 ...

  6. Linux 下使用静态google protocl buffer

    在Linux上编译google protobuff时,configure 默认选项是生成动态库,即libprotobuf.so文件.如果同时在多个动态库(动态库以dlopen方式动态加载)中使用同一b ...

  7. js 播放声音文件

    from:http://hi.baidu.com/xykking/item/7f64a04364e43ce4bdf45127 我在做项目的时候,也遇到了这个问题,可以灵活的使用下面的这两种方法,我使用 ...

  8. 使用Selectivizr让你的 CSS3选择器 通吃IE6/7/8

    说到HTML5,总是会让人不自觉的想到CSS3,貌似他们就应该是成双成对.OK!前几天和大家分享了<使用html5shiv让HTML5通吃IE6/7/8>,那今天,便再和大家分享一个能让H ...

  9. pycharm重置设置,恢复默认设置

    备忘,备忘,备忘 window 系统 找到下方目录-->删除. 再重新打开pycharm # Windows Vista, 7, 8, 10: <SYSTEM DRIVE>\User ...

  10. storm入门教程 第一章 前言

    转自:http://blog.linezing.com/?p=1847 storm:http://www.cnblogs.com/panfeng412/tag/Storm/ http://blog.l ...