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. Java更新

    Java I/O 总结 JVM(8):JVM知识点总览-高级Java工程师面试必备 细数JDK里的设计模式 Java中创建对象的5种不同方法 关于Java Collections的几个常见问题 类在什 ...

  2. 《Android应用性能优化》 第8章 图形

    1.例子中 30个部件的xml setContentView 几乎占用了从onCreate() 到 onResume() 结束之前所有时间的99% 因为展开布局的开销很大.要尽量用不同的布局方式.比如 ...

  3. 说说excel

    今天遇到一个实际问题. 我有一组数据: 0.0.0.1 activate.adobe.com 0.0.0.1 practivate.adobe.com 0.0.0.1 ereg.adobe.com 0 ...

  4. ARC100C Linear Approximation

    传送门 分析 这道题真的好水呀QwQ,想必大家都知道对于式子|x-2|+|x-3|x取什么值可以使式子结果最小,这道题也是这个原理,只需要将要额外减的1.2.3……提前减掉就行了. 代码 #inclu ...

  5. Git 之 初使用

    什么是Git? Git 是一个开源的分布式版本控制软件,用以有效.高速的处理从很小到非常大的项目版本管理. Git 最初是由Linus Torvalds设计开发的,用于管理Linux内核开发.Git ...

  6. 数据库commit问题

    对数据库进行修改后,需要commit!---之前也是忘记commit导致数据库反应不过来.

  7. Java Collection.RP

    在 Java2中,有一套设计优良的接口和类组成了Java集合框架Collection,使程序员操作成批的数据或对象元素极为方便.这些接口和类有很多对抽象数据类型操作的API,而这是我们常用的且在数据结 ...

  8. 共享库so

    so文件在linux中为共享库,与windows下的dll类似. so文件中的函数可供多个进程调用,最大可能的提供二进制代码的复用. 共享库可以使代码的维护工作大大简化,当修正了一些错误或者添加了新特 ...

  9. [译]javascript中的条件语句

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...

  10. [译]我们应该在HTML文档中何处放script标签

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...