Computer College college Student Program Design Contest (' collector ') the ' s puzzle

Source: Internet
Author: User

problem DescriptionThere is a collector who own many valuable jewels. He has a problem on how to store them. There is M special boxes. Each box has a value. And each of the N jewels have 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 t He value of j-th box.
Note that a box can store more than one jewel.
Now the collector turns to your for helping him to compute the minimal sum of differences. InputThere is multiple test cases.
The first line has both integers N, M (1<=n, m<=100000).
The second line has N integers, indicating the n jewels ' values.
The third line has m integers, indicating the m boxes ' values.
Each value was no more than 10000. OutputPrint One integer, indicating the minimal sum of differences. Sample Input4 41 2 3 44 3 2 14 41 2 3 41 1 1 1 Sample Output06

We sort first, and then we use the second array as the basis for the first set of data to check their position in the second group.

Lower_bound
A total of two cases, either just well found, one on the left
Like what
2 3 4 5 7 find 3 return 2
Find 6 return 5 We don't know if it's on the left or the right. The difference is small, can be considered in
Find 8 to return to the last position
#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>#defineMAXN (100000+10)#defineMAXM (100000)#defineINF 0X3FFFFFFF#defineINF 0x3f3f3f3f#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#defineLL Long Long#defineULL unsigned long Longusing namespacestd;intA[MAXN],B[MAXN];intMain () {intn,m;  while(cin>>n>>m) {intsum=0; inti,j;  for(i=1; i<=n;i++) {cin>>A[i]; }         for(i=1; i<=m;i++) {cin>>B[i]; } sort (A+1, a+n+1); Sort (b+1, b+m+1);  for(i=1; i<=n;i++)        {            intMid=inf; intAns=lower_bound (b +1, b+m+1, A[i])-b; //cout<<ans<<endl;            if(ans<=m) {Mid=min (Mid,abs (a[i]-B[ans])); }            if(ans>1) {Mid=min (Mid,abs (a[i]-b[ans-1])); } Sum+=mid; } cout<<sum<<Endl; }    return 0;}

Computer College college Student Program Design Contest (' collector ') the ' s puzzle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.