题解:AtCoder AT_awc0019_d Conservation Plan for the Botanical Garden
【题目来源】
AtCoder:D - Conservation Plan for the Botanical Garden
【题目描述】
Takahashi is the manager of a botanical garden. The garden contains \(N\) plants, each numbered from \(1\) to \(N\).
高桥是一个植物园的园长。这个植物园里有 \(N\) 株植物,每株编号从 \(1\) 到 \(N\)。
Each plant \(i\) (\(1 \leq i \leq N\)) has an "ornamental value" \(A_i\) and a "drought resistance" \(B_i\). Plants with higher ornamental value are more attractive to visitors, and plants with higher drought resistance are more resilient to water shortages.
每株植物 \(i\)(\(1 ≤ i ≤ N\))有一个"观赏价值" \(A_i\) 和一个"抗旱性" \(B_i\)。观赏价值越高的植物对游客越有吸引力,抗旱性越高的植物对缺水的适应能力越强。
An intense heatwave is expected this summer, and some plants may wither due to water shortage. Specifically, plants whose drought resistance \(B_i\) is at least the threshold \(T\) will survive without any intervention, but plants whose \(B_i\) is less than \(T\) will wither if no measures are taken.
预计今年夏天将有强烈的热浪,一些植物可能因缺水而枯萎。具体来说,抗旱性 \(B_i\) 不低于阈值 \(T\) 的植物无需任何干预即可存活,但 \(B_i\) 低于 \(T\) 的植物如果不采取措施就会枯萎。
To protect the plants that would wither, Takahashi has decided to install irrigation equipment on some plants. For each plant \(i\), he chooses whether or not to install irrigation equipment. Irrigation equipment can be installed on any plant (including plants whose drought resistance is at least \(T\)), and it is also allowed to install none at all. However, at most one irrigation equipment can be installed per plant. Installing irrigation equipment on plant \(i\) costs \(C_i\), and a plant with irrigation equipment installed will not wither regardless of its drought resistance value.
为了保护会枯萎的植物,高桥决定在一些植物上安装灌溉设备。对于每株植物 \(i\),他选择是否安装灌溉设备。灌溉设备可以安装在任何植物上(包括抗旱性不低于 \(T\) 的植物),也允许完全不安装。但是,每株植物最多只能安装一个灌溉设备。在植物 \(i\) 上安装灌溉设备的成本为 \(C_i\),安装了灌溉设备的植物无论其抗旱性如何都不会枯萎。
The total installation cost of the irrigation equipment must not exceed Takahashi's budget \(M\).
灌溉设备的总安装成本不得超过高桥的预算 \(M\)。
In summary, plant \(i\) survives (does not wither) if it satisfies either (or both) of the following conditions:
总之,植物 \(i\) 在满足以下任一(或全部)条件时存活(不枯萎):
- Irrigation equipment is installed on plant \(i\)
在植物 i 上安装了灌溉设备。 - Its drought resistance \(B_i\) is at least \(T\)
其抗旱性 \(B_i\) 不低于 \(T\)。
Plants that satisfy neither of these conditions will wither. Note that even if both conditions are satisfied simultaneously, the ornamental value is not counted twice.
不满足任一条件的植物将会枯萎。注意,即使同时满足两个条件,观赏价值也不会被重复计算。
Takahashi wants to maximize the total ornamental value of the surviving plants by choosing which plants to install irrigation equipment on, within the budget \(M\).
高桥希望在预算 \(M\) 内,通过选择在哪些植物上安装灌溉设备,来最大化存活植物的总观赏价值。
Find the maximum possible sum of ornamental values \(A_i\) over all surviving plants.
求所有存活植物的观赏价值 \(A_i\) 之和的最大可能值。
【输入】
\(N\) \(M\) \(T\)
\(A_1\) \(B_1\) \(C_1\)
\(A_2\) \(B_2\) \(C_2\)
\(\vdots\)
\(A_N\) \(B_N\) \(C_N\)
- The first line contains the number of plants \(N\), Takahashi's budget \(M\), and the drought resistance threshold \(T\), separated by spaces.
- The \(i\)-th of the following \(N\) lines contains the ornamental value \(A_i\), drought resistance \(B_i\), and irrigation equipment installation cost \(C_i\) of plant \(i\), separated by spaces.
【输出】
Output the maximum possible total ornamental value of the surviving plants on a single line.
【输入样例】
3 100 50
80 30 60
50 60 40
30 40 50
【输出样例】
130
【核心思想】
-
问题分析:给定 \(N\) 株植物,每株有观赏价值 \(A_i\)、抗旱性 \(B_i\)、灌溉成本 \(C_i\)。抗旱性 \(B_i \geq T\) 的植物无需灌溉即可存活(免费获得价值),\(B_i < T\) 的植物需要安装灌溉设备(花费 \(C_i\) 成本)才能存活。在预算 \(M\) 内,求存活植物的最大总观赏价值。这是一个01背包变体问题,关键在于处理"免费获得"的特殊情况。
-
算法选择:
- 二维DP:
dp[i][j]表示考虑前 \(i\) 个植物,花费成本为 \(j\) 时的最大观赏价值 - 分类处理:根据 \(B_i\) 与阈值 \(T\) 的关系,分为免费物品和需付费物品
- 二维DP:
-
关键步骤:
- 初始化:
dp[0][j] = 0,其余为0 - 状态转移:遍历每个植物 \(i\) 和每种成本 \(j\):
- 不选灌溉(但可能免费存活):
- 若 \(B_i \geq T\):
dp[i][j] = dp[i-1][j] + A[i](免费获得价值) - 若 \(B_i < T\):
dp[i][j] = dp[i-1][j](不存活,无价值)
- 若 \(B_i \geq T\):
- 选灌溉(需成本 \(C_i\)):若 \(j \geq C_i\),
dp[i][j] = max(dp[i][j], dp[i-1][j-C[i]] + A[i])
- 不选灌溉(但可能免费存活):
- 统计答案:遍历所有成本 \(j\),取
dp[n][j]的最大值
- 初始化:
-
时间/空间复杂度:
- 时间复杂度:\(O(N \times M)\)
- 空间复杂度:\(O(N \times M)\),可优化至 \(O(M)\)(滚动数组)
-
01背包变体(带免费物品条件)的核心思想:
- 分类处理:将物品分为两类:满足条件的免费获得,不满足条件的需要付费
- 免费物品的处理:不选时也获得价值(区别于普通01背包的不选则价值不变)
- 付费物品的处理:与普通01背包相同,选则占用容量并获得价值
- 状态转移的两种分支:免费获得分支和付费选择分支
- 适用于带条件免费获取的背包问题
【解题思路】

【算法标签】
01背包
【代码详解】
#include <bits/stdc++.h>
using namespace std;
const int N = 105, M = 10005;
int n, m, t; // n: 物品数量,m: 总容量限制,t: 阈值
int a[N], b[N], c[N]; // a[i]: 第i个物品的价值,b[i]: 第i个物品的属性,c[i]: 第i个物品的容量
int dp[N][M], ans; // dp[i][j]: 考虑前i个物品,容量为j时的最大价值,ans: 答案
int main()
{
cin >> n >> m >> t; // 读入物品数量、总容量限制、阈值
for (int i = 1; i <= n; i++)
{
cin >> a[i] >> b[i] >> c[i]; // 读入每个物品的价值、属性、容量
}
// 动态规划
for (int i = 1; i <= n; i++) // 遍历每个物品
{
for (int j = 0; j <= m; j++) // 遍历所有可能的容量
{
// 情况1:不选当前物品
if (b[i] >= t) // 如果当前物品的属性大于等于阈值t
{
dp[i][j] = dp[i - 1][j] + a[i]; // 获得这个物品的价值,不占用容量
}
else
{
dp[i][j] = dp[i - 1][j]; // 正常情况,不选则价值不变
}
// 情况2:选当前物品(需要容量足够)
if (j >= c[i]) // 如果容量足够选择当前物品
{
// 取两种情况的最大值
dp[i][j] = max(dp[i][j], dp[i - 1][j - c[i]] + a[i]);
}
}
}
// 寻找最大价值
for (int j = 0; j <= m; j++) // 遍历所有可能的容量
{
ans = max(ans, dp[n][j]); // 取考虑所有物品后的最大价值
}
cout << ans << endl; // 输出结果
return 0;
}
【运行结果】
3 100 50
80 30 60
50 60 40
30 40 50
130
浙公网安备 33010602011771号