poj 1036 Gangsters

一个dp题。

dp[i][j]表示i时刻state为j时的最大值。

刚开始超了内存,int换成short擦边过了(6336K)。

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", 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\n", res);
    }
    return 0;
}

你可能感兴趣的:(c)