UVA 672 - Gangsters(dp)

  Gangsters 

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 stoutnessSi. 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 is an integer M, then a blank line followed by M datasets. There is a blank line between datasets.

The first line of each dataset contains the values NK, and T, separated by spaces. ($1 \le N \le 100, 1 \le K \le 100, 0 \le T \le 30000$)

The second line of the dataset contains the moments of time when gangsters come to the restaurant$T_1, T_2, \dots, T_N$, separated by spaces. ( $0 \le T_i \le T$ for $i = 1, 2, \dots, N$)

The third line of the dataset contains the values of the prosperity of gangsters $P_1, P_2 , \dots, P_N$, separated by spaces. ( $0 \le P_i \le 300$ for $i = 1, 2, \dots, N$)

The forth line of the dataset contains the values of the stoutness of gangsters $S_1, S_2, \dots, S_N$, separated by spaces. ( $1 \le S_i \le K$ for $i = 1, 2, \dots, N$)


All values in the input file are integers.

Output 

For each dataset, print 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. Print a blank line between datasets.

Sample Input 

1

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

Sample Output 

26

题意:

有个酒店的门会改变尺寸,变化范围是【0,k】,这个门每秒钟尺寸可以变大1,可以减小1,也可以不变。

现在有n个人,他们的尺寸为si,每个人在ti时刻想要进入酒店,只有在ti时刻酒店门的尺寸恰好和这个人的尺寸大小相等,这个人才可以进入。

每个人有一个值Pi,当某人进入酒店,酒店就会增加Pi值。

在[0,T]这段时间内(0秒时酒店的门尺寸状态是0),求让一些人进入酒店,使得总Pi值最大。

思路:dp,定义dp[i]表示第i个人可以进入的情况的最大p值,状态转移方程为(if dp[j]状态存在并且dp[j]能到dp[i]状态) dp[i] = dp[j] + p[i];

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;

const int N = 105;
int T, n, k, t, i, j, dp[N];
struct Man {
	int t, p, s;
} m[N];

bool cmp(Man a, Man b) {
	return a.t < b.t;
}

int solve() {
	int ans = 0;
	sort(m + 1, m + n + 1, cmp);
	memset(dp, -1, sizeof(dp));
	dp[0] = 0;
	for (i = 1; i <= n; i++) {
		for (j = 0; j < i; j++) {
			int Time = m[i].t - m[j].t;
			int s = abs(m[i].s - m[j].s);
			if (s > Time || dp[j] == -1) continue;
			dp[i] = max(dp[i], dp[j] + m[i].p);
			
		}
		ans = max(ans, dp[i]);
	}
	return ans;
}

int main() {
	scanf("%d", &T);
	while (T--) {
		scanf("%d%d%d", &n, &k, &t);
		for (i = 1; i <= n; i++)
			scanf("%d", &m[i].t);
		for (i = 1; i <= n; i++)
			scanf("%d", &m[i].p);
		for (i = 1; i <= n; i++)
			scanf("%d", &m[i].s);
		printf("%d\n", solve());
		if (T) printf("\n");
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值