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;
}