[CEOI2018]Cloud computing 背包dp

本文介绍了一种使用背包动态规划算法解决特定资源分配问题的方法。问题涉及到多个具有不同属性的电脑,需要根据一系列需求选择合适的电脑组合,使得整体性能最大化。文章详细解释了如何通过调整输入数据和应用动态规划技巧来高效地解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description
有n个电脑,电脑有三个属性s,f,v。
有m个要求,每个要求也有三个属性,S,F,V要求选出若干个电脑,使它们s总和大于S,并且每台电脑f大于F,一台电脑在一个要求中用过就不可放到其他要求中。
求总能完成要求总V-所选电脑总v最大。


Sample Input
4
4 2200 700
2 1800 10
20 2550 9999
4 2000 750
3
1 1500 300
6 1900 1500
3 2400 4550


Sample Output
350


首先将电脑和要求的f都按从大到小顺序排序,
将要求看作一个物品,将其s取反。
将电脑看作一个物品,将其v取反。
做背包DP,注意一些边界问题。
时间复杂度其实挺高的???gay过


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
LL _max(LL x, LL y) {return x > y ? x : y;}
int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}

struct node {
	int c, f, v;
} a[4100];
LL f[110000];

bool cmp(node a, node b) {
	if(a.f == b.f) return a.v < b.v;
	return a.f > b.f;
}

int main() {
	int n = read();
	for(int i = 1; i <= n; i++) a[i].c = read(), a[i].f = read(), a[i].v = -read();
	int m = read();
	for(int i = n + 1; i <= n + m; i++) a[i].c = read(), a[i].f = read(), a[i].v = read();
	sort(a + 1, a + n + m + 1, cmp);
	int sum = 0;
	memset(f, 128, sizeof(f)); f[0] = 0;
	for(int i = 1; i <= n + m; i++) {
		if(a[i].v < 0) {
			for(int j = sum; j >= 0; j--) f[j + a[i].c] = _max(f[j + a[i].c], f[j] + a[i].v);
			sum += a[i].c;
		} else {
			for(int j = 0; j <= sum + a[i].c; j++) f[j] = _max(f[j], f[j + a[i].c] + a[i].v);
		}
	} LL ans = 0;
	for(int i = 0; i <= sum; i++) ans = _max(ans, f[i]);
	printf("%lld\n", ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值