惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
B
Blog
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Jina AI
Jina AI
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
L
LangChain Blog
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
Y
Y Combinator Blog
F
Fortinet All Blogs
H
Help Net Security
B
Blog RSS Feed
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
PAT顶级 1002.Business|OhYee 博客
2018-07-04 · via OhYee 博客

题目

原题链接
{% fold 点击显/隐题目 %}

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

{% endfold %}

解析

01背包问题,需要注意的是对于某个项目,是还是不选
按照动态规划问题的套路,先找维度物品的序号任务期限
也即dp[i][j]为前i个任务在j天内能达到的最大收益。

根据背包问题的两种情况,有:
选:dp[i][j] = dp[i-1][j-pro.l] + pro.p
不选:dp[i][j] = dp[i-1][j]
很显然,如果要选择,存在两个条件:

  1. j-pro.l > 0
  2. j <= pro.d

根据样例可以发现,对于一群项目,我们应该优先处理deadline较前的任务(贪心的思想)

最后,将所有的dp值中最大的取出来即可
因为存在由于deadline的限制导致最大值不在边界的情况。如:

3
2 4 5
1 2 9
9 1 2

可以使用滚动数组再次压缩dp数组,不过内存给的比较大,没有压缩

代码

C++解法

{% fold 点击显/隐代码 %}

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

#define Log(format, ...) // printf(format, ##__VA_ARGS__)

struct Node {
    int p, l, d;
    Node(int _p = 0, int _l = 0, int _d = 0) : p(_p), l(_l), d(_d) {}
    void read() {
        scanf("%d%d%d", &p, &l, &d);
        Log("read %d %d %d\n", p, l, d);
    }
    bool operator<(const Node &rhs) const {
        if (d == rhs.d)
            return l < rhs.l;
        return d < rhs.d;
    }
};

const int maxn = 55;

Node projects[maxn];

int main() {
    int n;
    scanf("%d", &n);

    int maxd = 0;
    for (int i = 1; i <= n; ++i) {
        projects[i].read();
        maxd = max(maxd, projects[i].d);
    }
    Log("maxd %d\n", maxd);

    int **dp = new int *[n + 1];
    for (int i = 0; i <= n; ++i) {
        dp[i] = new int[maxd + 1];
        memset(dp[i], 0, sizeof(int) * (maxd + 1));
    }

    sort(projects + 1, projects + 1 + n);

    int maxProfit = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= maxd; ++j) {
            dp[i][j] = dp[i - 1][j]; // 不选
            Node pro = projects[i];
            if (j - pro.l >= 0 && j <= pro.d)
                dp[i][j] = max(dp[i][j], dp[i - 1][j - pro.l] + pro.p); // 选
            maxProfit = max(maxProfit, dp[i][j]);
            Log("dp[%d][%d] = %d\t(%d,%d,%d)\n", i, j, dp[i][j], pro.p, pro.l,
                pro.d);
        }
    }

    printf("%d\n", maxProfit);

    for (int i = 0; i <= n; ++i)
        delete[] dp[i];
    delete[] dp;

    return 0;
}

{% endfold %}