首页 > 题解 > poj 1036 Gangsters

poj 1036 Gangsters

终于一道DP

传送门

Description

N gangsters are going to a restaurant. The i-th gangster comes at the time Ti and has the prosperity Pi. The door of the restaurant has K+1 states of openness expressed by the integers in the range [0, K]. The state of openness can change by one in one unit of time; i.e. it either opens by one, closes by one or remains the same. At the initial moment of time the door is closed (state 0). The i-th gangster enters the restaurant only if the door is opened specially for him, i.e. when the state of openness coincides with his stoutness Si. If at the moment of time when the gangster comes to the restaurant the state of openness is not equal to his stoutness, then the gangster goes away and never returns.
The restaurant works in the interval of time [0, T].
The goal is to gather the gangsters with the maximal total prosperity in the restaurant by opening and closing the door appropriately.
Input

?The first line of the input file contains the values N, K, and T, separated by spaces. (1 <= N <= 100 ,1 <= K <= 100 ,0 <= T <= 30000 )
?The second line of the input file contains the moments of time when gangsters come to the restaurant T1, T2, …, TN, separated by spaces. ( 0 <= Ti <= T for i = 1, 2, …, N)
?The third line of the input file contains the values of the prosperity of gangsters P1, P2, …, PN, separated by spaces. ( 0 <= Pi <= 300 for i = 1, 2, …, N)
?The forth line of the input file contains the values of the stoutness of gangsters S1, S2, …, SN, separated by spaces. ( 1 <= Si <= K for i = 1, 2, …, N)
All values in the input file are integers.
Output

Print to the output file the single integer ?the maximal sum of prosperity of gangsters in the restaurant. In case when no gangster can enter the restaurant the output should be 0.
Sample Input

4 10 20
10 16 8 16
10 11 15 1
10 7 1 8
Sample Output

26

题意

有众多歹徒在各个时刻来到一个酒馆(t1、t2、t3。。。tn)。两个歹徒可以在同一时刻到来。每个歹徒有两样属性——身上带钱的数量和自身肥胖程度。酒馆的门是自动伸缩的,空间从0-K。每一个时间单位,该门可以有三种选择——(1)不变(2)空间增大1(3)空间缩小1。当且仅当歹徒来时,歹徒肥胖程度和门打开程度相等才让进入。歹徒一旦无法进入,就赚到钱了。门和时间的初始状态均为0。求酒店在固定时间0-T内最多能赚到多少钱。

方程

背包= =

dp[i][j] = max{dp[i-1][j-1], dp[i-1][j+1], dp[i-1][j]} + w

细节:每次判断一下是否能达到此状态

代码

#include <cstdio>
#include <algorithm>
using namespace std;
struct per
{
    int t,p,s;
}p[1000];
bool comp(per a,per b)
{
    return a.t<b.t;
}
int abs(int a){return a<0?-a:a;}
int dp[1000],maxs;
int n,k,T;
main()
{
    scanf("%d%d%d",&n,&k,&T);
    for (int i=1;i<=n;i++)
        scanf("%d",&p[i].t);
    for (int i=1;i<=n;i++)
        scanf("%d",&p[i].p);
    for (int i=1;i<=n;i++)
        scanf("%d",&p[i].s);

    sort(p,p+n+1,comp);

    for (int i=1;i<=n;i++)
    {
        if (p[i].t>T)
            break;
        if (p[i].s>p[i].t)
            continue;
        for (int j=0;j<i;j++)
        {
            if (abs(p[i].t-p[j].t)>=abs(p[i].s-p[j].s))
                if (dp[i]<dp[j]+p[i].p)
                    dp[i]=dp[j]+p[i].p;
            if (dp[i]>maxs)
                maxs=dp[i];
        }
    }
    printf("%d",maxs);
}