作为一道蓝色的DP神题,它成功把我卡了两个小时。
这个题对我来说确实有些困难,本人太菜了,对DP毫无感觉。
后来发现这是个背包,要么吃要么放,前提是饿不死。
于是:
AC代码:
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; inline int read() { int x = 0; int k = 1; char c = getchar(); while (!isdigit(c)) if (c == '-') k = -1, c = getchar(); else c = getchar(); while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return k * x; } int m, n, dp[105][3000]; struct node { int t, f, h; bool operator < (const node & a) const { return t < a.t; } }rbs[102]; int main() { m = read(); n = read(); memset(dp, -1, sizeof(dp)); for (int i = 1; i <= n; ++i) rbs[i].t = read(), rbs[i].f = read(), rbs[i].h = read(); sort(rbs + 1, rbs + n + 1); dp[0][0] = 10; for (int i = 0; i <= n; ++i) for (int j = 0; j <= m; ++j) { if (dp[i][j] < 0) continue; if (dp[i][j] >= rbs[i + 1].t - rbs[i].t && j + rbs[i + 1].h >= m) { printf("%d", rbs[i + 1].t); return 0; } if (dp[i][j] >= rbs[i + 1].t - rbs[i].t) dp[i + 1][j + rbs[i + 1].h] = dp[i][j] - rbs[i + 1].t + rbs[i].t, dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + rbs[i + 1].f - rbs[i + 1].t + rbs[i].t); } int sum = 0, mogic = 10; for (int i = 1; i <= n; ++i) if (mogic >= rbs[i].t - rbs[i - 1].t) mogic -= rbs[i].t - rbs[i - 1].t, mogic += rbs[i].f, sum += rbs[i].t - rbs[i - 1].t; else { printf("%d", sum + mogic); return 0; } printf("%d", sum + mogic); return 0; }