题意:每个人有a, b两个值,现在从n个人选出m个人,使得a和b的总和之差绝对值最小,如果有多解,输出a和b总和的和最大的解
思路:dp[i][j][k]表明选到第i个人,选了j个人,差为k的解的总和最大值,然后转移最后找一下解即可
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
const int N = 205;
const int M = 25;
const int INF = 0x3f3f3f3f;
int n, m, d[N], p[N], dp[N][M][805], path[N][M][805], out[M], on;
int main() {
int cas = 0;
while (~scanf("%d%d", &n, &m) && n || m) {
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
for (int k = 0; k <= 800; k++)
dp[i][j][k] = -INF;
dp[0][0][400] = 0;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &d[i], &p[i]);
for (int j = 0; j <=m; j++) {
for (int k = 0; k <= 800; k++) {
if (dp[i][j][k] < dp[i - 1][j][k]) {
dp[i][j][k] = dp[i - 1][j][k];
path[i][j][k] = 0;
}
if (j == 0) continue;
int pre = k - d[i] + p[i];
if (pre < 0 || pre > 800 || dp[i - 1][j - 1][pre] == -INF) continue;
if (dp[i][j][k] < dp[i - 1][j - 1][pre] + d[i] + p[i]) {
dp[i][j][k] = dp[i - 1][j - 1][pre] + d[i] + p[i];
path[i][j][k] = i;
}
}
}
}
int ans = INF, ansv;
for (int i = 0; i <= 800; i++) {
if (dp[n][m][i] != -INF && ans > abs(i - 400)) {
ans = abs(i - 400);
ansv = i;
} else if (ans == abs(i - 400)) {
if (dp[n][m][i] > dp[n][m][ansv])
ansv = i;
}
}
int ans1 = 0, ans2 = 0;
on = 0;
while (n) {
int tmp = path[n][m][ansv];
if (tmp) {
out[on++] = tmp;
n--;
m--;
ansv = ansv - d[tmp] + p[tmp];
ans1 += d[tmp];
ans2 += p[tmp];
} else
n--;
}
printf("Jury #%d\n", ++cas);
printf("Best jury has value %d for prosecution and value %d for defence:\n", ans1, ans2);
for (int i = on - 1; i >= 0; i--)
printf(" %d", out[i]);
printf("\n\n");
}
return 0;
}