1828. Minimal
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
There are two sets S1 and S2 subjecting to:
(1) S1, S2 are both the subsets of {x | x is an integer and 0 < x < 1,000,000}
(2) 0 < |S1 | < |S2| < 500
F(S1 ,S2) = min {|a1-b1| + |a2 – b2| + … + | aN –bN |}
in which ai ∈S1, bi ∈S2
ai ≠aj if i≠j
bi ≠bj if i≠j
(i, j = 1, 2 … N,N = | S1|)
Input
The first line contains an integer indicating the number of test cases.
For each test case, there are two integers N and M in the first line. N is the number of elements in S1 while M is the number of elements in S2. There are N+M lines that follow. In the first N lines are the integers in S1, while
the last M lines S2.There is NO bland line between two cases.
Output
For each test case, print the result of F(S1 ,S2), one case per line. There is NO bland line between two cases.
Sample Input
1 2 3 30 20 50 10 40
Sample Output
20
// Problem#: 1828
// Submission#: 3342649
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
#include <cmath>
using namespace std;
const int MAX_N = 505;
const int INF = 99999999;
int a[MAX_N], b[MAX_N];
int dp[MAX_N][MAX_N];
int main() {
std::ios::sync_with_stdio(false);
int caseNum;
cin >> caseNum;
while (caseNum--) {
int N, M;
cin >> N >> M;
for (int i = 1; i <= N; i++) cin >> a[i];
for (int i = 1; i <= M; i++) cin >> b[i];
sort(a + 1, a + N + 1);
sort(b + 1, b + M + 1);
for (int i = 1; i <= N; i++) {
if (i == 1) dp[i][i] = abs(a[i] - b[i]);
else dp[i][i] = dp[i - 1][i - 1] + abs(a[i] - b[i]);
}
for (int i = 1; i <= N; i++) {
for (int j = i + 1; j <= M; j++) {
dp[i][j] = min(dp[i - 1][j - 1] + abs(a[i] - b[j]), dp[i][j - 1]);
}
}
cout << dp[N][M] << endl;
}
//getchar();
//getchar();
return 0;
}