Problem Description
There is a collector who own many valuable jewels. He has a problem about how to store them. There are M special boxes. Each box has a value. And each of the N jewels has a value too. 
The collector wants to store every jewel in one of the boxs while minimizing the sum of difference value. 
The difference value between a jewel and a box is: |a[i] - b[j]|, a[i] indicates the value of i-th jewel, b[j] indicates the value of j-th box.
Note that a box can store more than one jewel.
Now the collector turns to you for helping him to compute the minimal sum of differences.
Input
There are multiple test cases.
For each case, the first line has two integers N, M (1<=N, M<=100000).
The second line has N integers, indicating the N jewels’ values.
The third line have M integers, indicating the M boxes’ values.
Each value is no more than 10000.
Output
Print one integer, indicating the minimal sum of differences.
Sample Input
4 4
1 2 3 4
4 3 2 1
4 4
1 2 3 4
1 1 1 1
Sample Output
0
6

我们先排序,然后以第二个数组为基础,第一组的数据去查他们在第二组的位置

lower_bound
一共两种情况,要么刚刚好找到,一个在左边
比如
2 3 4 5 7 查找3 返回2
查找 6 返回5 我们不知道它离左边还是右边差值小,可以都考虑进去
查找8 返回最后一个位置

#include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define MAXN (100000+10)
#define MAXM (100000)
#define inf 0x3fffffff
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std;
int a[MAXN],b[MAXN];
int main()
{
int n,m;
while(cin>>n>>m)
{
int sum=;
int i,j;
for(i=;i<=n;i++)
{
cin>>a[i];
}
for(i=;i<=m;i++)
{
cin>>b[i];
}
sort(a+,a+n+);
sort(b+,b+m+);
for(i=;i<=n;i++)
{
int mid=inf;
int ans=lower_bound(b+,b+m+,a[i])-b;
// cout<<ans<<endl;
if(ans<=m)
{
mid=min(mid,abs(a[i]-b[ans]));
}
if(ans>)
{
mid=min(mid,abs(a[i]-b[ans-]));
}
sum+=mid;
}
cout<<sum<<endl;
}
return ;
}

计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle的更多相关文章

  1. hdu 计算机学院大学生程序设计竞赛(2015’11)

    搬砖 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submissi ...

  2. 计算机学院大学生程序设计竞赛(2015’11)1005 ACM组队安排

    1005 ACM组队安排 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Pro ...

  3. 计算机学院大学生程序设计竞赛(2015’12)Study Words

    Study Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. 计算机学院大学生程序设计竞赛(2015’12)Polygon

    Polygon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. 计算机学院大学生程序设计竞赛(2015’12)The Country List

    The Country List Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. 计算机学院大学生程序设计竞赛(2015’12) 1008 Study Words

    #include<cstdio> #include<cstring> #include<map> #include<string> #include&l ...

  7. 计算机学院大学生程序设计竞赛(2015’12) 1009 The Magic Tower

    #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using ...

  8. 计算机学院大学生程序设计竞赛(2015’12) 1006 01 Matrix

    #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> ...

  9. 计算机学院大学生程序设计竞赛(2015’12) 1003 The collector’s puzzle

    #include<cstdio> #include<algorithm> using namespace std; using namespace std; +; int a[ ...

  10. 计算机学院大学生程序设计竞赛(2015’12) 1004 Happy Value

    #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include ...

随机推荐

  1. request 10.0 模块安装

     https://pypi.python.org/packages/49/6f/183063f01aae1e025cf0130772b55848750a2f3a89bfa11b385b35d7329d ...

  2. 定位程序问题出现的原因工具-jstack

    jstack还可以生成线程快照 如何使用jstack: 1.打开命令行,输入jstack 在任务管理器中就可看到对应进程id 2.在命令行中输入 jstack -l 进程id 这样就可得到进程中所有的 ...

  3. QT5环境搭建

    https://blog.csdn.net/liang19890820/article/details/53931813

  4. ROS Learning-030 (提高篇-008 A Mobile Base-06) 控制移动平台 --- (Python)odom导航的例子:移动一个方块路径

    ROS 提高篇 之 A Mobile Base-06 - 控制移动平台 - (Python)再次使用odom导航的一个例子:移动一个方块路径 我使用的虚拟机软件:VMware Workstation ...

  5. Arduino 002 --- 在Ubuntu(Linux) 中搭建Arduino开发环境

    在Ubuntu/Linux 中搭建Arduino开发环境 我的Ubuntu系统:Ubuntu 14.04.10 TLS 32位 需要安装的Arduino的版本:Arduino 1.6.11(最新版本) ...

  6. java中public static void main(String[] args)中String[] args代表什么意思?

    这是java程序的入口地址,java虚拟机运行程序的时候首先找的就是main方法.跟C语言里面的main()函数的作用是一样的.只有有main()方法的java程序才能够被java虚拟机欲行,可理解为 ...

  7. DesertWind TopCoder - 1570

    传送门 分析 首先我们要知道为了情况最坏,无论你到哪,一定会在你前往绿洲的最短路上的那片沙子上刮风,所以这个点到绿洲的最短路即为他相连的点中到绿洲距离最短的距离+3和次短的距离+1的最小值,具体实现见 ...

  8. xml元素类型PCDATA和CDATA的区别(DTD中)

      PCDATAPCDATA 的意思是被解析的字符数据(parsed character data).可把字符数据想象为 XML 元素的开始标签与结束标签之间的文本.PCDATA 是会被解析器解析的文 ...

  9. Ubuntu中安装LAMP

    现在,很多人可能已经用上ubuntu了,大家可能花了大量时间在ubuntu的美化上,这无可厚非,但是,ubuntu应该给我们的工作和学习带来更多的便利和方便.ubuntu作为linux,为我们提供了强 ...

  10. Sass和Compass设计师指南 Ben Frain 中文高清PDF扫描版​

    Sass和Compass设计师指南是<响应式Web设计:HTML5和CSS3实战>作者Ben Frain的又一力作.作者通过丰富.完整的案例,循序渐进地展示了Sass和Compass的使用 ...