标签:style blog http color os io
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> using namespace std; #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #define INF 0x3f3f3f3f const int N = 505; int t, n1, n2, i, j, k; double a1[N], a2[N], dp[N][N]; void tra() { if (n1 > n2) { for (int i = 1; i <= n1; i++) { double t = a1[i]; a1[i] = a2[i]; a2[i] = t; } int t = n1; n1 = n2; n2 = t; } k = n2 - n1; } int main() { scanf("%d", &t); while (t--) { double ans = INF; scanf("%d%d", &n1, &n2); for (i = 1; i <= n1; i++) scanf("%lf", &a1[i]); for (i = 1; i <= n2; i++) scanf("%lf", &a2[i]); tra(); sort(a1 + 1, a1 + n1 + 1); sort(a2 + 1, a2 + n2 + 1); for (i = 1; i <= n1; i++) { double Min = INF; for (j = 0; j <= k; j++) { if (i + j > n2) break; Min = min(Min, dp[i - 1][j]); dp[i][j] = INF; dp[i][j] = min(dp[i][j], Min + fabs(a1[i] - a2[i + j])); if (i == n1) ans = min(ans, dp[i][j]); } } printf("%.1lf\n", ans); } return 0; }
UVA 1534 - Taekwondo(dp),码迷,mamicode.com
标签:style blog http color os io
原文地址:http://blog.csdn.net/accelerator_/article/details/24841605