挺有趣的一道dp题目,看上去接近于0/1背包,但是考虑到取每个点时间不同会对最后结果产生影响,因此需要进行预处理
对于物品x和物品y,当时间为p时,先加x后加y的收益为
a[x]-(p+c[x])*b[x]+a[y]-(p+c[x]+c[y])*by
而先加y再加x的收益为
a[y]-(p+c[y])*b[y]+a[x]-(p+c[y]+c[x])*bx
化简这两个式子,不难发现对于x和y,如果满足
c[x]*b[y]<c[y]*b[x]
,那么x 一定优于 y
由以上推论即可得解,对于题目中所给的物品,将其按照以上顺序排序,在进行0/1背包,即可得解
#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn = 100000 + 100;
struct data {
long long ai, bi, ci;
};
data p[60];
long long dp[maxn];
int t, n;
bool cmp(data aa, data bb) {
return (aa.ci * bb.bi < aa.bi * bb.ci);
}
int main () {
scanf("%d %d", &t, &n);
for (int i = 1; i <= n; i++) scanf("%lld", &p[i].ai);
for (int i = 1; i <= n; i++) scanf("%lld", &p[i].bi);
for (int i = 1; i <= n; i++) scanf("%lld", &p[i].ci);
std :: sort(p + 1, p + n + 1, cmp);
for (int i = 1; i <= n; i++)
for (int j = t; j >= p[i].ci; j--)
dp[j] = std :: max(dp[j], dp[j - p[i].ci] + (p[i].ai - j * p[i].bi));
long long ans = 0;
for (int i = 1; i <= t; i++) ans = std :: max(ans, dp[i]);
printf("%lld", ans);
return 0;
}
当时做这题时想尝试多次贪心取最优值的办法,,然而,最后只得了30分,虽然尝试未成功,但是面对贪心题目时,这也不失为一种方法
附上乱搞代码
#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn = 50 + 10;
int T, n;
struct data {
int ai;
int bi;
int ci;
};
data p[maxn];
int dp[100000 + 10];
bool cmp1(data aa, data bb) {
return(aa.bi < bb.bi);
}
bool cmp2(data aa, data bb) {
return (aa.ci < bb.ci);
}
bool cmp3(data aa, data bb) {
return (aa.ai > bb.ai);
}
bool cmp4(data aa, data bb) {
return (aa.bi * aa.ci < bb.bi * bb.ci);
}
int main () {
scanf("%d %d", &T, &n);
for (int i = 1; i <= n; i++) scanf("%d", &p[i].ai);
for (int i = 1; i <= n; i++) scanf("%d", &p[i].bi);
for (int i = 1; i <= n; i++) scanf("%d", &p[i].ci);
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = T; j >= p[i].ci; j--) {
dp[j] = std :: max(dp[j],dp[j - p[i].ci] + (p[i].ai - j * p[i].bi));
}
}
for (int i = 1; i <= T; i++) ans = std :: max(ans, dp[i]);
std :: sort(p + 1, p + n + 1, cmp1);
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
for (int j = T; j >= p[i].ci; j--) {
dp[j] = std :: max(dp[j],dp[j - p[i].ci] + (p[i].ai - j * p[i].bi));
}
}
for (int i = 1; i <= T; i++) ans = std :: max(ans, dp[i]);
std :: sort(p + 1, p + n + 1, cmp2);
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
for (int j = T; j >= p[i].ci; j--) {
dp[j] = std :: max(dp[j],dp[j - p[i].ci] + (p[i].ai - j * p[i].bi));
}
}
for (int i = 1; i <= T; i++) ans = std :: max(ans, dp[i]);
std :: sort(p + 1, p + n + 1, cmp3);
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
for (int j = T; j >= p[i].ci; j--) {
dp[j] = std :: max(dp[j],dp[j - p[i].ci] + (p[i].ai - j * p[i].bi));
}
}
for (int i = 1; i <= T; i++) ans = std :: max(ans, dp[i]);
std :: sort(p + 1, p + n + 1, cmp4);
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
for (int j = T; j >= p[i].ci; j--) {
dp[j] = std :: max(dp[j],dp[j - p[i].ci] + (p[i].ai - j * p[i].bi));
}
}
for (int i = 1; i <= T; i++) ans = std :: max(ans, dp[i]);
printf("%d", ans);
return 0;
}