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.

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

题意: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); 
	}
}

转载于:https://www.cnblogs.com/Hilda/archive/2012/07/31/2616706.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值