题意:n个歹徒进饭店,可变化宽度的门,范围[0, k],每个歹徒进门在ti时间进门,身材si,进去后有pi的成功值,问最大的成功值
分析:首先按照进门时间排序,dp[i][j] 表示第i个歹徒在门大小为j的时候进门的最大成功值,那么状态转移方程:dp[i][j] = dp[i-1][k] + a[i].p 条件是门大小从k到j的时间差小于i和ii-1人进门时间差
收获:这类有条件限制的dp转移,我分类为条件DP
代码:
/************************************************ * Author :Running_Time * Created Time :2015-8-31 16:19:35 * File Name :UVA_672.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int N = 1e2 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; struct People { int t, p, s; bool operator < (const People &r) const { return t < r.t; } }a[N]; int dp[N][N]; int main(void) { int T; scanf ("%d", &T); while (T--) { int n, door, time; scanf ("%d%d%d", &n, &door, &time); for (int i=1; i<=n; ++i) scanf ("%d", &a[i].t); for (int i=1; i<=n; ++i) scanf ("%d", &a[i].p); for (int i=1; i<=n; ++i) scanf ("%d", &a[i].s); sort (a+1, a+1+n); memset (dp, -1, sizeof (dp)); int ans = 0; dp[0][0] = 0; for (int i=1; i<=n; ++i) { for (int j=0; j<=door; ++j) { for (int k=0; k<=door; ++k) { if (dp[i-1][k] == -1 || abs (j - k) > a[i].t - a[i-1].t) continue; if (a[i].s == j && j) { dp[i][j] = max (dp[i][j], dp[i-1][k] + a[i].p); } else { dp[i][j] = max (dp[i][j], dp[i-1][k]); } ans = max (ans, dp[i][j]); } } } printf ("%d\n", ans); if (T) puts (""); } return 0; }
时间: 2024-10-01 01:26:46