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

推荐订阅源

N
News and Events Feed by Topic
S
Security @ Cisco Blogs
S
Secure Thoughts
Attack and Defense Labs
Attack and Defense Labs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Hacker News: Front Page
博客园 - 叶小钗
H
Heimdal Security Blog
Microsoft Security Blog
Microsoft Security Blog
Forbes - Security
Forbes - Security
AI
AI
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
罗磊的独立博客
Application and Cybersecurity Blog
Application and Cybersecurity Blog
爱范儿
爱范儿
GbyAI
GbyAI
The Last Watchdog
The Last Watchdog
TaoSecurity Blog
TaoSecurity Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
D
DataBreaches.Net
Recent Announcements
Recent Announcements
Schneier on Security
Schneier on Security
C
Cisco Blogs
美团技术团队
D
Docker
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
月光博客
月光博客
雷峰网
雷峰网
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
Arctic Wolf
B
Blog RSS Feed
Cisco Talos Blog
Cisco Talos Blog
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
V2EX - 技术
V2EX - 技术
Y
Y Combinator Blog
N
News and Events Feed by Topic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
W
WeLiveSecurity
Security Archives - TechRepublic
Security Archives - TechRepublic
G
GRAHAM CLULEY
Jina AI
Jina AI
Hugging Face - Blog
Hugging Face - Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News

distjr_的博客

最新 | 画中少女 | distjr_的博客 天津行记 | distjr_的博客 最终选择去天津大学了 | distjr_的博客 上伊那牡丹,看完了 | distjr_的博客 上伊那牡丹,看完了 | distjr_的博客 帖子测试 | distjr_的博客 最新 | 关于我的高中三年 | distjr_的博客 最新 | KeepOpen:一款保持移动硬盘开启状态的小工具 | distjr_的博客 最新 | KeepOpen:一款保持移动硬盘开启状态的小工具 | distjr_的博客 最新 | 关于我的高中三年 | distjr_的博客 一款基于Python的随机学生点名器 | distjr_的博客 最新 | 一款基于Python的随机学生点名器 | distjr_的博客 U571839 千本桜 题解 | distjr_的博客 U571839 千本桜 题解 | distjr_的博客 土地 | distjr_的博客 土地 | distjr_的博客 一九六八年 | distjr_的博客 一九六八年 | distjr_的博客 原野 | distjr_的博客 原野 | distjr_的博客 一种基于人工智能技术的系统发生树绘制与物种演化路径推断系统 | distjr_的博客 一种基于人工智能技术的系统发生树绘制与物种演化路径推断系统 | distjr_的博客 回来?Ⅱ | distjr_的博客 回来?Ⅱ | distjr_的博客 U314392 distjr_想买书 题解 | distjr_的博客 天涯边 | distjr_的博客 天涯边 | distjr_的博客 CF509D Restoring Numbers 题解 | distjr_的博客 CF509D Restoring Numbers 题解 | distjr_的博客 回来?Ⅰ | distjr_的博客 回来?Ⅰ | distjr_的博客 一场提前告知的自杀 | distjr_的博客 一场提前告知的自杀 | distjr_的博客 test | distjr_的博客 test | distjr_的博客 置顶 | 关于我自己 | distjr_的博客 置顶 | 关于我自己 | distjr_的博客
U314392 distjr_想买书 题解 | distjr_的博客
文章作者: distjr_ · 2024-02-18 · via distjr_的博客

思路

首先是一个 dp 的板子,设 $dp_j$ 为花费 $j$ 元能获得的最大的期待值。

对于每次转移,都可以选择买哪一本书,一旦买了一本书,花费的钱增加 $p_i$ ,获得的总期待值增加 $v_i$ 。

故不难看出状态转移方程为 $dp_j = \max \lbrace dp_{j-p_i}+v_i \rbrace $。

但题目还要求输出方案,所以开一个 vector 记录方案,每次找到更优的方案时,就在上一个方案的后面补上当前新增加的物品,由于输入是单调的,自然保证输出是单调的。

同时,还要设置一个标记数组 flag 以筛去不合法的答案,确保所有找到的方案都是正常计算得来的,否则可能会出现一些奇奇怪怪的bug。

感谢 linmou 提供的 std!

std

/**
 * @file buy.cpp
 * @brief Solution of Luogu U314392
 * @date 2024-02-18
 */
#include <cstdio>
#include <vector>
#define MAXN 505
#define MAXM 500005
using namespace std;

int n, m, a, maxx = 0, v[MAXM], p[MAXM], dp[MAXM], ans;
bool flag[MAXN][MAXM];
vector<int> programme;

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d %d %d", &a, &p[i], &v[i]);
    flag[n + 1][0] = 1;
    for (int i = n; i >= 1; i--)
    {
        for (int j = 0; j <= m; j++)
            flag[i][j] = flag[i + 1][j];
        for (int j = m; j >= p[i]; j--)
        {
            if (!flag[i + 1][j - p[i]])
                continue;
            if (dp[j] <= v[i] + dp[j - p[i]]) // 状态转移
            {
                dp[j] = v[i] + dp[j - p[i]];
                flag[i][j] = flag[i][j] | flag[i + 1][j - p[i]]; // 标记
            }
        }
    }
    for (int i = 1; i <= m; i++) // 选出最优方案
        if (maxx <= dp[i])
            maxx = dp[i], ans = i;
    int i = 1, j = ans, sum = 0;
    while (i <= n && j)
    {
        if (flag[i][j] && flag[i][j - p[i]])
        {
            printf("%d ", i);
            sum += p[i];
            j -= p[i];
        }
        i++;
    }
    return 0;
}