计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle
来源:互联网 发布:大华pss监控软件 编辑:程序博客网 时间:2024/06/05 20:09
The collector’s puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 944 Accepted Submission(s): 210
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.
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.
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 44 3 2 14 41 2 3 41 1 1 1
Sample Output
06
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int a[10005],b[10005];int main(){ int N,M,t; while(~scanf("%d%d",&N,&M)) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(int i=0; i<N; ++i) { scanf("%d",&t); ++a[t]; } for(int i=0; i<M; ++i) { scanf("%d",&t); ++b[t]; } long long tol=0; for(int i=0; i<10005; ++i) if(a[i]>0) { int j; for(j=0;; ++j) { if(i-j>0&&b[i-j]>0)break; if(i+j<10005&&b[i+j]>0)break; } tol=tol+a[i]*j; } printf("%lld\n",tol); } return 0;}
1 0
- 计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle
- 计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle
- HDU计算机学院大学生程序设计竞赛(2015’12)1003 The collector’s puzzle
- 计算机学院大学生程序设计竞赛(2015’12)(STL )
- 计算机学院大学生程序设计竞赛(2015’12)Polygon
- 计算机学院大学生程序设计竞赛(2015’12)Happy Value
- 计算机学院大学生程序设计竞赛(2015’12)Bitwise Equations
- 计算机学院大学生程序设计竞赛(2015’12)01 Matrix
- 计算机学院大学生程序设计竞赛(2015’12)Pick Game
- 计算机学院大学生程序设计竞赛(2015’12)Study Words
- 计算机学院大学生程序设计竞赛(2015’12)Bitwise Equations
- 计算机学院大学生程序设计竞赛(2015’12)The Magic Tower(水~~~)
- hdu 计算机学院大学生程序设计竞赛(2015’12)The Country List
- hdu 计算机学院大学生程序设计竞赛(2015’12)The Magic Tower
- 计算机学院大学生程序设计竞赛(2015’12)The Country List
- 计算机学院大学生程序设计竞赛(2015’12)The Magic Tower
- 计算机学院大学生程序设计竞赛(2015’12)The Country List(水题)
- 计算机学院大学生程序设计竞赛(2015’12)1001 The Country List
- Objective C运行时(runtime)技术的几个要点总结
- SDWebImage
- php链接mysql数据库
- Gradle导入jar
- nginx+tomcat的keepalive验证、bio/nio连接比较
- 计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle
- 欢迎使用CSDN-markdown编辑器
- 时间戳计算的问题
- linux(非root) 开机启动tomcat和80端口无法开启解决方案
- Java 中 字节流 和 字符流的区别(转自郝斌 Java 教学)
- 人脸检测研究
- mysql的外键约束条件及注意事项
- typedef给数据类型起别名
- android popupWindow