P1017--Liqeuer
Time limit: 1000MS
Memory Limit: 65536KB
Description
The thud, the two piles of parts and a lava "river" are shown in front of you, and we define the first set of parts as the collection S1, and the second heap as the set S2.
First, the number of S1 parts is less than the number of parts equal to S2. We now set N as the number of S1 parts, from the S2 to select n parts, so that the matching difference of two sets is the smallest. Such a bridge can be built smoothly through the magma "river".
The matching difference for two sets is described in this definition: definition f (s1,s2) =min (|a1-b1|+|a2-b2|+|a3-b3|+...+|an-bn|) {N is the number of S1 elements, and ai∈s1,bi∈s2},f (S1,S2) is the matching difference of two sets.
Input Format
The first line is a number testcase, which indicates the number of test data groups (0<=TESTCASE<=5)
The format of each group of data is as follows: The first row of two numbers n1,n2 (0<=n1<=n2<=500), N1 represents the number of parts in the first heap, N2 represents the number of parts in the second heap.
Next N1 lines, one number per line, representing the individual elements of S1 (no more than 10000). The next N2 line, one number per line, represents the individual elements of S2 (also less than 10000).
Output Format
Output testcase lines, one number per line. The first line represents the minimum value of the matching difference between the two sets of data in Group I.
Sample Input
310 1012345678920101112131415161718194 51234567898 12346810162125293425124235621931494637
Sample Output
8216129
Hint
Dynamic programming of "solving the puzzle" sequence
F[I][J] represents the minimum F value that S1 matches to the i,s2 match to J
f[i][j]= (i==j)? (F[i-1][j-1]+abs (A[I]-B[J)): (Min (f[i][j-1], F[i-1][j-1]+abs (A[i]-b[j])))
1#include <stdio.h>2#include <algorithm>3#include <string.h>4 using namespacestd;5 intn1,n2,f[510][510];6 inta[510],b[510];7 intAbsintx) {returnX>0? x:-x;}8 intMain () {9 intTest scanf"%d",&test);Ten while(test--) { OneN1=n2=0; Memset (A,0,sizeof(a)); memset (b,0,sizeof(b)); Memset (F,0,sizeof(f)); Ascanf"%d%d",&n1,&n2); - for(intI=1; i<=n1;++i) scanf ("%d",&a[i]); - for(intI=1; i<=n2;++i) scanf ("%d",&b[i]); theSort (A +1, a+n1+1); Sort (b +1, b+n2+1); - for(intI=1; i<=n1;++i) - for(intj=i;j<=n2-n1+i;++j) - if(I==J) f[i][j]=f[i-1][j-1]+abs (a[i]-b[j]); + ElseF[i][j]=min (f[i][j-1],f[i-1][j-1]+abs (a[i]-b[j])); -printf"%d\n", f[n1][n2]); + } A return 0; at}
View Code
[Fzyzoj 1017] Liqeuer