当前位置: 首页 > news >正文

题解:AtCoder AT_awc0001_d Merchant on the Highway

本文分享的必刷题目是从蓝桥云课洛谷AcWing等知名刷题平台精心挑选而来,并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构,旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。

欢迎大家订阅我的专栏:算法题解:C++与Python实现!

附上汇总贴:算法竞赛备考冲刺必刷题(C++) | 汇总


【题目来源】

AtCoder:D - Merchant on the Highway

【题目描述】

Takahashi is a merchant doing business along a highway that extends from east to west. There areN NNtowns lined up in a row along this highway, numbered1 , 2 , . . . , N 1, 2, ..., N1,2,...,Nfrom west to east. Doing business in thei ii-th town yields a profit ofA i A_iAi, but costsB i B_iBiyen as accommodation expenses.
高桥是一位沿着东西向高速公路经商的商人。这条公路上有一排N NN个城镇,自西向东编号为1 , 2 , . . . , N 1, 2, ..., N1,2,...,N。在第i ii个城镇经商可获得A i A_iAi的利润,但需要花费B i B_iBi日元作为住宿费用。

Takahashi has decided to select some of these towns to do business in. However, Takahashi’s carriage has the following restriction:
高桥决定从这些城镇中选择一部分进行经商。然而,高桥的马车有以下限制:

  • The towns visited do not need to have consecutive numbers, but when the visited town numbers are arranged in ascending order, the difference between any two adjacent numbers must be at mostK KK. In other words, if the visited town numbers in ascending order arep 1 , p 2 , … , p m p_1,p_2,…,p_mp1,p2,,pm, thenp j + 1 − p j ≤ K p_{j+1}−p_j≤Kpj+1pjKmust hold for all1 ≤ j ≤ m − 1 1≤j≤m−11jm1. This is because the carriage has a limit on the distance it can travel at once.
    访问的城镇编号无需连续,但当按升序排列访问的城镇编号时,任意两个相邻编号之差必须至多为K KK。换言之,如果按升序排列的访问城镇编号为p 1 , p 2 , … , p m p_1, p_2, …, p_mp1,p2,,pm,则对于所有1 ≤ j ≤ m − 1 1 ≤ j ≤ m-11jm1,必须满足p j + 1 − p j ≤ K p_{j+1} - p_j ≤ Kpj+1pjK。这是因为马车单次可行驶的距离有限制。

The total accommodation expenses Takahashi can prepare isM MMyen. When selecting towns such that the total accommodation expenses of visited towns is at mostM MMyen, find the maximum possible total profit.
高桥能够准备的住宿费用总额为M MM日元。在满足访问城镇的总住宿费用不超过M MM日元的条件下选择城镇时,求可能的最大总利润。

Note that if no town is visited, the total profit is0 00.
注意,如果未访问任何城镇,总利润为0 00

【输入】

N NNM MMK KK

A 1 A_1A1B 1 B_1B1

A 2 A_2A2B 2 B_2B2

⋮ \vdots

A N A_NANB N B_NBN

  • The first line containsN NNrepresenting the number of towns,M MMrepresenting the total accommodation expenses available, andK KKrepresenting the maximum number of towns that can be traveled at once, separated by spaces.
  • From the 2nd line to the( N + 1 ) (N+1)(N+1)-th line, information about each town is given.
  • The( 1 + i ) (1+i)(1+i)-th line contains the profitA i A_iAiobtainable in thei ii-th town and the accommodation expenseB i B_iBi, separated by spaces.

【输出】

Output in one line the maximum possible total profit when selecting towns that satisfy the conditions.

【输入样例】

5 10 2 8 3 5 4 10 5 3 2 7 3

【输出样例】

21

【解题思路】

【算法标签】

#01背包#

【代码详解】

#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=205;intn,m,k,ans;// n: 物品数量,m: 预算,k: 允许的最大物品间隔,ans: 最大总价值intdp[N][N][N];// 动态规划数组 dp[考虑了前i个物品][使用了j的钱][最后一个购买的物品编号是p] = 最大总价值structNode{intid,A,B;// id: 物品编号,A: 物品价值,B: 物品价格}a[N];signedmain(){cin>>n>>m>>k;// 读入物品数量、预算、最大间隔限制for(inti=1;i<=n;i++){cin>>a[i].A>>a[i].B;// 读入物品的价值和价格a[i].id=i;// 记录物品编号}// 动态规划求解for(inti=1;i<=n;i++)// 逐个考虑每个物品{for(intj=0;j<=m;j++)// 枚举使用的预算{for(intp=0;p<=n;p++)// 枚举最后一个购买的物品编号{// 不购买当前第i个物品dp[i][j][p]=max(dp[i][j][p],dp[i-1][j][p]);// 购买当前第i个物品的条件:// 1. 预算足够 (j >= a[i].B)// 2. 与上一个购买物品的间隔不超过k (a[i].id - a[p].id <= k)// 注意:当p=0时,a[0].id未定义,但a[i].id - 0 <= k 总是成立if(j>=a[i].B&&a[i].id-a[p].id<=k){// 状态转移:从dp[i-1][j-a[i].B][p]转移而来// 表示前i-1个物品花费j-a[i].B,最后一个购买的是物品p// 然后购买当前第i个物品,最后一个购买的物品变为idp[i][j][i]=max(dp[i][j][i],dp[i-1][j-a[i].B][p]+a[i].A);}}}}// 在所有状态中寻找最大总价值for(intj=0;j<=m;j++)// 枚举所有可能的预算使用情况{for(intp=0;p<=n;p++)// 枚举最后一个购买物品的所有可能{ans=max(ans,dp[n][j][p]);// 取最大值}}cout<<ans<<endl;// 输出答案return0;}

【运行结果】

5 10 2 8 3 5 4 10 5 3 2 7 3 21
http://www.jsqmd.com/news/689194/

相关文章:

  • 老项目维护必备:在Windows Server 2022上完美部署SQL Server 2012全攻略
  • 想给孩子说的话(1):警惕成长路上的陷阱
  • 室内动捕+Position模式:为你的PX4无人机开启‘上帝视角’PID自整定
  • DeepL翻译浏览器扩展:让外语内容阅读变得轻松自然
  • WinUtil:终极Windows管理工具,让你的电脑从此告别繁琐设置
  • 法国和非盟在会计核算、会计科目等方面的法律和政策要求完全不同,因为它们的性质截然不同:法国是一个主权国家,而非盟是一个政府间国际组织
  • 2026解锁学习神器,让娃主动爱上学习 - 品牌测评鉴赏家
  • 150块捡漏RK3399盒子AM40:从安卓到Firefly Linux的保姆级刷机教程(含TTL接线图)
  • Webpack Encore 入门指南:10分钟快速搭建现代前端构建流程
  • 技术支持管理中的服务台建设
  • 向量点乘与叉乘
  • **类脑计算新范式:用Python实现脉冲神经网络模拟与生物启发式学习机制**在人工智能快速演进
  • 2026解锁小学生学习新姿势!这些APP让孩子主动爱上学习 - 品牌测评鉴赏家
  • 维谛EMU10触摸屏监控模块用户手册
  • Linux环境下用LeRobot实现主从臂数据采集:从配置到避坑全流程
  • 题解:AtCoder AT_awc0001_e Temperature Fluctuation Range
  • NHSE:动物森友会存档编辑工具全面指南
  • 从UE到核心网:一文拆解Logged MDT与Immediate MDT在4G/5G中的完整数据流与避坑指南
  • 揭秘论文优化新利器:书匠策AI,让降重与去AIGC痕迹变得如此简单!
  • 2026年北京保洁公司口碑推荐:北京开荒保洁、北京厂房保洁、北京保洁清洗、北京外墙清洗、北京地毯清洗空调清洗公司口碑选择指南 - 海棠依旧大
  • 如何5分钟完成视频字幕提取:Video-subtitle-extractor终极指南
  • 如何高效使用MarkDownload:专业网页剪辑与Markdown转换终极指南
  • 题解:AtCoder AT_awc0002_a Organizing the Bookshelf
  • Qwen3.5-27B多场景落地:跨境电商商品图识别、短视频封面图文分析
  • AI投毒情报预警 | Xinference国产推理框架遭受供应链窃密后门投毒
  • C语言简单笔记
  • 英特尔发布至强600系列工作站处理器与锐炫Pro B70 GPU,双芯联动重塑AI工作站格局
  • 从零到一:Tomcat深度实战 - 适合系列教程
  • IPSAS(国际公共部门会计准则)和法国的PCG(会计总方案)在收入确认上的核心区别,源于它们所遵循的根本会计模型和适用对象的不同
  • 豆包 linux-6.19/tools/accounting/delaytop.c 源码分析