2015‘12杭电校赛1003 The collector’s puzzle(双指针水题)
2016-01-06 21:48
351 查看
题解:
可以说读题读不懂么…排个序然后直接双指针就可以了,水题
代码
#include <cstdio> #include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #include <vector> #include <map> #include <set> #include <ctime> #include <cmath> #include <cctype> #include <string> #include <bitset> #define MAX 100000 #define LL long long const int INF = 999999; using namespace std; int cas=1,T,n,m,a[MAX+10],b[MAX+10]; int main() { while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=m;i++) scanf("%d",&b[i]); int sum=0; sort(a+1,a+n+1); sort(b+1,b+m+1); b[0]=-INF; b[m+1] =INF; int i=1,j=1; while(i<=n) { while(a[i]>b[j]) j++; sum+=min(abs(a[i]-b[j-1]),abs(b[j]-a[i])); i++; } printf("%d\n",sum); } return 0; }
题目
The collector’s puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1079 Accepted Submission(s): 244
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 41 2 3 4
4 3 2 1
4 4
1 2 3 4
1 1 1 1
Sample Output
06
相关文章推荐
- php实现文件上传
- 车辆管理系统之分析信息建表(一)
- makefile--嵌套执行(四)
- ubuntu ns-3编译
- linux下使用无线网卡的命令行方
- 再见2015,你好2016
- OpenStack 配置
- pragma code_seg
- 在Windows环境下使用MinGW编译Qt 4.8.6
- 堆和栈的区别
- TypeError: window.clipboardData is undefined
- poj3660
- 搜索命令之locate,whereis,which
- SQL循环语句
- ireport各个版本的下载地址
- 解决sqlite删除数据后,文件大小不变问题(VACUUM)
- Qt 工程 pro文件
- linux-scp命令
- face++ c++接口 以及 curl 库的使用,json库在另一个博客介绍
- 2015’12杭电校赛1002 Polygon (计算几何)