2097 Problem I 毕业bg

本文介绍了一种解决毕业季参与多个团体bg(散伙饭)时间规划问题的算法,通过给定每个bg的快乐度、持续时间和发起人离校时间,使用动态规划求解个人在有限时间内可获得的最大快乐度。

问题 I: 毕业bg

时间限制: 1 Sec  内存限制: 32 MB
提交: 81  解决: 27
[提交][状态][讨论版][命题人:外部导入]

题目描述

每 年毕业的季节都会有大量毕业生发起狂欢,好朋友们相约吃散伙饭,网络上称为“bg”。参加不同团体的bg会有不同的感觉,我们可以用一个非负整数为每个 bg定义一个“快乐度”。现给定一个bg列表,上面列出每个bg的快乐度、持续长度、bg发起人的离校时间,请你安排一系列bg的时间使得自己可以获得最 大的快乐度。

例如有4场bg:
第1场快乐度为5,持续1小时,发起人必须在1小时后离开;
第2场快乐度为10,持续2小时,发起人必须在3小时后离开;
第3场快乐度为6,持续1小时,发起人必须在2小时后离开;
第4场快乐度为3,持续1小时,发起人必须在1小时后离开。
则获得最大快乐度的安排应该是:先开始第3场,获得快乐度6,在第1小时结束,发起人也来得及离开;再开始第2场,获得快乐度10,在第3小时结束,发起人正好来得及离开。此时已经无法再安排其他的bg,因为发起人都已经离开了学校。因此获得的最大快乐度为16。

注意bg必须在发起人离开前结束,你不可以中途离开一场bg,也不可以中途加入一场bg。
又因为你的人缘太好,可能有多达30个团体bg你,所以你需要写个程序来解决这个时间安排的问题。

输入

测试输入包含若干测试用例。每个测试用例的第1行包含一个整数N (<=30),随后有N行,每行给出一场bg的信息:
    h l t
其中 h 是快乐度,l是持续时间(小时),t是发起人离校时间。数据保证l不大于t,因为若发起人必须在t小时后离开,bg必须在主人离开前结束。

当N为负数时输入结束。

输出

每个测试用例的输出占一行,输出最大快乐度。

样例输入

3
6 3 3
3 2 2
4 1 3
4
5 1 1
10 2 3
6 1 2
3 1 1
-1

样例输出

7
16
#include<iostream>
#include<algorithm>
using namespace std;

struct BG {
	int h, l, t;//快乐度,持续时间,离开时间
}bg[50];

bool cmp(BG a, BG b) {//必须先离开的在前
	return a.t < b.t;
}
int main() {
	int N;
	while (cin >> N&&N > 0) {
		int dp[1000] = { 0 };
		for (int i = 0; i < N; i++) {
			cin >> bg[i].h >> bg[i].l >> bg[i].t;
		}
		sort(bg, bg + N, cmp);
		for (int i = 0; i < N; i++) {
			for (int j = bg[i].t; j >= bg[i].l; j--) {
				dp[j] = max(dp[j], dp[j - bg[i].l] + bg[i].h);
			}
		}
		cout << *max_element(dp, dp + bg[N - 1].t + 1) << endl;
	}
	return 0;
}

 

Dynamic programming is an algorithm suitable for solving the Knapsack problem. It avoids repeated calculations by decomposing complex problems into overlapping sub - problems and storing the solutions to the sub - problems. The core of dynamic programming is state definition and state transition equations [^1]. The general steps of using dynamic programming to solve the Knapsack problem are as follows: 1. **Define the state**: Usually, two - dimensional states are defined. For example, let `dp[i][j]` represent the maximum value that can be obtained when considering the first `i` items and the capacity of the knapsack is `j`. 2. **State transition equation**: For the 0 - 1 Knapsack problem, if the weight of the `i` - th item is `w[i]` and the value is `v[i]`, then the state transition equation is: - When `j < w[i]`, `dp[i][j]=dp[i - 1][j]` (the current item cannot be put into the knapsack). - When `j >= w[i]`, `dp[i][j]=max(dp[i - 1][j], dp[i - 1][j - w[i]]+v[i])` (choose whether to put the current item into the knapsack). Here is a simple Python code example for the 0 - 1 Knapsack problem: ```python def knapsack(weights, values, capacity): n = len(weights) dp = [[0 for _ in range(capacity + 1)] for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, capacity + 1): if j < weights[i - 1]: dp[i][j] = dp[i - 1][j] else: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weights[i - 1]] + values[i - 1]) return dp[n][capacity] weights = [2, 3, 4, 5] values = [3, 4, 5, 6] capacity = 8 print(knapsack(weights, values, capacity)) ``` ### Application scenarios - **Resource allocation**: In project management, given a limited amount of resources (such as time, budget, manpower), and different tasks with different resource requirements and benefits, the Knapsack problem can be used to select the most profitable combination of tasks. - **Stock selection**: When an investor has a certain amount of funds and there are multiple stocks with different prices and expected returns, the Knapsack problem can be used to select the most profitable stock portfolio.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值