POJ 1036Gangsters【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.
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.
?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
题意:N个土匪,伸缩门的范围是K, 时间T, 伸缩门在【0, k】范围内变动,每个单位时间可以不变伸长或者缩短一个单位。给出每个最烦到达的时刻,取得的成就,和肥胖程度。即如果伸缩门的长度和土匪的肥胖程度一样,即得到成就。
思路:即门的长度和前一时刻门的长度+1, 长度-1, 不变有关即dp[i][j]表示i时刻门的长度j时取得的最大成就,dp[i][j]=max{dp[i-1][j], dp[i-1][j-1], dp[i-1][j+1]};
代码如下:
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; int f[2][102]; struct people { int t, s, pp; }; int cmp(people x, people y) { return x.t<y.t; } int main() { int n, r, time, i, j, k, ii; while(scanf("%d%d%d", &n, &k, &time)!=EOF) { people p[102]; int visit[30002]; memset(visit, 0, sizeof(visit)); for(i=1; i<=n; i++) { scanf("%d", &p[i].t); visit[p[i].t]=1; } for(i=1; i<=n; i++) scanf("%d", &p[i].pp); for(i=1; i<=n; i++) scanf("%d", &p[i].s); sort(p+1, p+n+1, cmp); memset(f, 0, sizeof(f)); for(i=0; i<=time; i++) for(j=0; j<=k&&j<=i; j++) { int value=0; if(visit[i]==1) { for(ii=1; ii<=n; ii++) if(j==p[ii].s&&p[ii].t==i) value+=p[ii].pp; } if(j==0) f[i%2][j]=max(f[1-i%2][j], f[1-i%2][j+1])+value; else if(j==k) f[i%2][j]=max(f[1-i%2][j], f[1-i%2][j-1])+value; else f[i%2][j]=max(max(f[1-i%2][j], f[1-i%2][j-1]), f[1-i%2][j+1])+value; } int maxnum=0; for(i=0; i<=k; i++) if(f[time%2][i]>maxnum) maxnum=f[time%2][i]; printf("%d\n", maxnum); } }