POJ 1015 Jury Compromise(DP)

题意:每个人有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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值