Pie
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 683 Accepted Submission(s): 188
Problem Description
A lot of boys and girls come to our company to pie friends. After we get their information, we need give each of them an advice for help. We know everyone’s height, and we believe that the less difference of a girl and a boy has, the better it is. We need to find as more matches as possible, but the total difference of the matches must be minimum.
Input
The input consists of multiple test cases. The first line of each test case contains two integers, n, m (0 < n, m <= 10000), which are the number of boys and the number of girls. The next line contains n float numbers, indicating the height of each boy. The last line of each test case contains m float numbers, indicating the height of each girl. You can assume that |n – m| <= 100 because we believe that there is no need to do with that if |n – m| > 100. All of the values of the height are between 1.5 and 2.0. The last case is followed by a single line containing two zeros, which means the end of the input.
Output
Output the minimum total difference of the height. Please take it with six fractional digits.
Sample Input
2 3
1.5 2.0
1.5 1.7 2.0
0 0
Sample Output
0.000000
Author
momodi@whu
Source
Recommend
notonlysuccess
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 7 using namespace std; 8 9 int n,m; 10 double a[10010],b[10010]; 11 double dp[2][120]; 12 13 int main(){ 14 15 //freopen("input.txt","r",stdin); 16 17 while(~scanf("%d%d",&n,&m)){ 18 if(n==0 && m==0) 19 break; 20 int i,j,k; 21 if(n<=m){ 22 for(i=1;i<=n;i++) 23 scanf("%lf",&a[i]); 24 for(i=1;i<=m;i++) 25 scanf("%lf",&b[i]); 26 }else{ 27 for(i=1;i<=n;i++) 28 scanf("%lf",&b[i]); 29 for(i=1;i<=m;i++) 30 scanf("%lf",&a[i]); 31 int tmp=n; 32 n=m; 33 m=tmp; 34 } 35 sort(a+1,a+n+1); 36 sort(b+1,b+m+1); 37 memset(dp,0,sizeof(dp)); 38 int len=m-n+1; 39 for(i=1;i<=n;i++){ 40 dp[i%2][1]=dp[(i-1)%2][1]+fabs(a[i]-b[i]); 41 for(k=2;k<=len;k++){ 42 j=i+k-1; 43 dp[i%2][k]=min(dp[(i-1)%2][k]+fabs(a[i]-b[j]),dp[i%2][k-1]); 44 } 45 } 46 printf("%.6lf\n",dp[n%2][len]); 47 } 48 return 0; 49 }