计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle
来源:互联网 发布:时时彩合买软件 编辑:程序博客网 时间:2024/06/18 15:55
The collector’s puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 986 Accepted Submission(s): 217
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 <cstring>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <queue>#include <stack>#include <set>#include <map>#include <list>#define LL long long#define INF 0x3f3f3f3fusing namespace std;int a[100100],b[100100];int main(){ int n,m; while(cin>>n>>m) { for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<m;i++) cin>>b[i]; sort(a,a+n); sort(b,b+m); int ans = 0; int i=0, j=0; while(i<n && j<m) { if(a[i] <= b[j]) { if(j >= 1) { if(a[i] - b[j-1] >= b[j] - a[i]) ans += (b[j] - a[i++]); else ans += (a[i++] - b[j-1]); } else ans += (b[j]-a[i++]); } else j++; } while(i < n) ans += fabs(a[i++] - b[m-1]); cout<<ans<<endl; } return 0;}
0 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
- Hive中的三种不同的数据导出方式介绍
- ios中ARC模式下的内存管理
- [leetcode] 58. Length of Last Word 解题报告
- DP---源点到目的点经过最多的1点
- 关于CommandType.StoredProcedure输出参数访问问题
- 计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle
- 第六届福建省大学生程序设计竞赛 Problem J RunningMan【数学】
- Hadoop编码解码【压缩解压缩】机制详解(1)
- xcode command+左键不能跳转的解决方法
- LeetCode之String题目汇总
- print_r、echo、var_dump三者的区别
- LeetCode 058 Length of Last Word
- 数据中心虚拟化为何离不开大二层网络技术?
- <四>创建Lists和Cards