poj 1036 Gangsters

2335 ワード

一つのdp問題.
dp[i][j]は、i時刻stateがjのときの最大値を表す.
最初はメモリを超えていたので、intをshortに変えてエッジを拭きました(6336 K).
dp[i][j]=max(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1])に、i時刻が到来する値を加算する.
/*
 * Author: stormdpzh
 * POJ: 1036 Gangsters
 * Created Time: 2012/5/18 10:57:41
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>

#define sz(v) ((int)(v).size())
#define rep(i, n) for(int i = 0; i < n; i++)
#define repf(i, a, b) for(int i = a; i <= b; i++)
#define repd(i, a, b) for(int i = a; i >= b; i--)
#define out(n) printf("%d
", n) #define wh(n) while(scanf("%d", &n) != EOF) #define whz(n) while(scanf("%d", &n) != EOF && n != 0) #define lint long long using namespace std; const short MaxN = 105; const int MaxT = 30005; short n, k, t; short ti[MaxN], pi[MaxN], si[MaxN]; short dp[MaxT][MaxN]; bool hash[MaxT]; short maxx(short a, short b, short c) { return (a > b) ? (a > c ? a : c) : (b > c ? b : c); } void init() { memset(hash, false, sizeof(hash)); rep(i, n) { scanf("%d", &ti[i + 1]); hash[ti[i + 1]] = true; } rep(i, n) scanf("%d", &pi[i + 1]); rep(i, n) scanf("%d", &si[i + 1]); memset(dp, 0, sizeof(dp)); } short gao() { if(hash[0]) repf(i, 1, n) { if(ti[i] == 0 && si[i] == 0) dp[0][0] = max(dp[0][0], pi[i]); } repf(i, 1, t) { dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]); repf(j, 1, k - 1) { dp[i][j] = maxx(dp[i - 1][j - 1], dp[i - 1][j], dp[i - 1][j + 1]); } dp[i][k] = max(dp[i - 1][k], dp[i - 1][k - 1]); if(hash[i]) repf(j, 1, n) { if(ti[j] == i && si[j] <= i) dp[i][si[j]] += pi[j]; } } short tmp = 0; rep(i, k) tmp = max(tmp, dp[t][i]); return tmp; } int main() { while(scanf("%d%d%d", &n, &k, &t) != EOF) { init(); short res = gao(); printf("%d
", res); } return 0; }