1002 Business (35 分)

As the manager of your company, you have to carefully consider, for each project, the time taken to finish it, the deadline, and the profit you can gain, in order to decide if your group should take this project. For example, given 3 projects as the following:

  • Project[1] takes 3 days, it must be finished in 3 days in order to gain 6 units of profit.
  • Project[2] takes 2 days, it must be finished in 2 days in order to gain 3 units of profit.
  • Project[3] takes 1 day only, it must be finished in 3 days in order to gain 4 units of profit.

You may take Project[1] to gain 6 units of profit. But if you take Project[2] first, then you will have 1 day left to complete Project[3] just in time, and hence gain 7 units of profit in total. Notice that once you decide to work on a project, you have to do it from beginning to the end without any interruption.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤50), and then followed by N lines of projects, each contains three numbers P, L, and D where P is the profit, L the lasting days of the project, and D the deadline. It is guaranteed that L is never more than D, and all the numbers are non-negative integers.

Output Specification:

For each test case, output in a line the maximum profit you can gain.

Sample Input:

4
7 1 3
10 2 3
6 1 2
5 1 1

Sample Output:

18

题目大意

‎作为公司的经理,您必须仔细考虑每个项目完成该项目所需的时间,截止日期以及可以获得的利润,以便决定您的团队是否应该接受此项目。例如,给定 3 个项目如下:‎

  • ‎项目[1]需要3天,必须在3天内完成才能获得6个单位的利润。‎
  • ‎项目[2]需要2天,必须在2天内完成才能获得3个单位的利润。‎
  • ‎项目[3]只需要1天,必须在3天内完成才能获得4个单位的利润。‎

‎你可以服用项目[1]来获得6个单位的利润。但是,如果你先参加项目[2],那么你将有1天的时间及时完成项目[3],从而总共获得7个单位的利润。请注意,一旦你决定处理一个项目,你必须从头到尾不间断地完成它。‎

‎输入规格:‎

‎每个输入文件包含一个测试用例。对于每种情况,第一行给出一个正整数‎N (≤50‎),然后跟‎N‎项目行,每个包含三个数字‎P‎,‎L‎和‎D‎哪里‎P‎是利润,‎L‎项目的持续时间,以及‎D‎截止日期。可以保证‎L‎永远不会超过‎D‎,并且所有数字都是非负整数。‎

‎输出规格:‎

‎对于每个测试用例,在一行中输出您可以获得的最大利润。‎

‎示例输入:‎

<span style="color:#404040"><code class="language-in">4
7 1 3
10 2 3
6 1 2
5 1 1
</code></span>

‎示例输出:‎

 

18

思路及代码

#include <bits/stdc++.h>
using namespace std;
struct Pro {
    int p, l, d;
};
int main() {
    int n;
    cin >> n;
    vector<Pro> pros(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> pros[i].p >> pros[i].l >> pros[i].d;
        t = max(t, pros[i].d);
    }
    sort(pros.begin(), pros.end(), [](const Pro& p1, const Pro& p2) {
        return p1.d < p2.d;
    });
    int t = pros.back().d;
    vector<vector<int>> dp(n + 1, vector<int>(t + 1));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= t; ++j) {
            dp[i][j] = dp[i - 1][j];
            int k = min(j, pros[i].d) - pros[i].l;
            if (k >= 0)
                dp[i][j] = max(dp[i][j], dp[i - 1][k] + pros[i].p);
        }
    }
    cout << dp[n][t];
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值