PAT (Top Level) Practice1002 Business

题目链接:https://pintia.cn/problem-sets/994805148990160896/problems/994805156145643520

很入门的动态规划题,一开始需要先排序。

算法:dp/背包

#include
#include
#include
#include
#include
using namespace std;
#define N 55
struct node {
	int P, L, D;
}cases[N];
int dp[N][10000];
bool cmd(node x, node y) {
	return x.D < y.D;
}
int main() {
	int n;
	int mx = -1;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d%d%d", &cases[i].P, &cases[i].L, &cases[i].D);
		mx = max(mx, cases[i].D);
	}
	sort(cases+1, cases + n+1,cmd);
	int mxx = -1;
	memset(dp, 0, sizeof(dp));
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= mx; j++) {
			if (cases[i].L <= j) {
				if (cases[i].D >= j)
					dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - cases[i].L] + cases[i].P);
				else
					dp[i][j] = dp[i][cases[i].D];
			}
			else {
				dp[i][j] = dp[i - 1][j];
			}
			//cout << i << " " << j << " " << dp[i][j] << endl;
			mxx = max(mxx, dp[i][j]);
		}
	}
	cout << mxx << endl;
	return 0;
}

 

你可能感兴趣的:(算法)