目录
问题描述
旅行者穿越沙漠的过程中需要不断地消耗携带的饮用水,到达终点前会经过几个绿洲,每个绿洲均设有水分补给站可以为旅行者提供水分补给并收取一定的费用。
沿途共有 n 个补给站,每个补给站收取的费用都一样,但是提供的水量不尽相同。起点到终点的距离为 D 公里,position[i] 表示第 i 个补给站距离起点的距离,单位为公里,supply[i] 表示第 i 个补给站可以提供的水量,单位为升。
假设旅行者在起点时携带了 W 升的水,每行走 1 公里需要消耗 1 升的水量,身上可携带的水量没有上限,且携带的水量多少不会对体能消耗产生影响,鉴于每次进补给站花费的钱都是一样多,期望用最少的补给次数到达终点,请帮忙计算最少的补给次数。
输入格式
第一行输入整数 D 和 W, D 表示起点到终点的距离,W 表示初始携带的水量
第二行输入数组 position,长度为 N,分别表示 N 个补给站分别距离起点的距离
第三行输入数组 supply,长度为 N, 分别表示 N 个补给站分别可以供给的水量
数据约束
- 1 <= D, W <= 10^8, 0 <= N <= 1000, 0 < position[i], supply[i] < D
输出格式
输出一个整数表示最少的补给次数,若无法到达终点则返回 -1
输入样例
10 4
1 4 7
6 3 5
输出样例
1
样例解释
起点到终点共 10 公里,初始时携带 4 升水,途径 3 个补给站。只需在第 1 个补给站补给一次获得 6 升水,即可走完全程
解题思路:
问题理解
旅行者需要穿越一段距离为 D
公里的沙漠,初始携带 W
升水。沿途有 n
个补给站,每个补给站可以提供不同量的水。旅行者每行走 1 公里需要消耗 1 升水。目标是使用最少的补给次数到达终点。
数据结构选择
- 补给站信息:我们可以使用两个数组
position
和supply
来存储每个补给站的位置和提供的水量。 - 当前状态:我们需要记录当前的位置、剩余的水量以及已经使用的补给次数。
算法步骤
- 初始化:从起点开始,初始水量为
W
,补给次数为 0。 - 遍历补给站:从第一个补给站开始,检查是否能够到达该补给站。如果不能到达,则返回 -1。
- 选择补给站:在能够到达的补给站中,选择一个最优的补给站进行补给。最优的选择策略是选择能够提供最多水量的补给站,这样可以减少后续的补给次数。
- 更新状态:到达补给站后,更新当前的水量和补给次数。
- 重复步骤:继续遍历下一个补给站,直到到达终点。
- 终点判断:如果到达终点时水量足够,则返回补给次数;否则返回 -1。
关键点
- 贪心策略:在每个补给站选择时,优先选择能够提供最多水量的补给站,这样可以最大化后续的行走距离,从而减少补给次数。
- 边界条件:需要处理无法到达任何补给站或无法到达终点的情况。
- 特判: 测试点7就是当w>d的时候,属于特殊情况,完全不需要补给,所以直接返回0即可
代码实现:
1.初始化:
需要有记录当前位置,当前水携带量以及补给次数和存放位置和补给数量的两个数组的结构体数组
struct Station {
int pos;
int sup;
};
int current_position = 0;
int current_water = w;
int supply_count = 0;
vector<Station> stations;
for (int i = 0; i < position.size(); ++i) {
stations.push_back({position[i], supply[i]});
}
2. 计算与下一补给点的距离:
可以到达的话则对两个变量进行更新,反之则无法到达,返回-1;
int distance_to_next = stations[i].pos - current_position;
// 如果当前水量不足以到达下一个补给站,返回 -1
if (current_water < distance_to_next) {
return -1;
}
current_position = stations[i].pos;
current_water -= distance_to_next;
3.选择最优补给点:
核心在于贪心,只要有能到达并且补给量最大的补给站,那就进行补给。所以我们只需要简单做个遍历即可。
// 选择最优的补给站进行补给
int max_supply = 0;
int max_index = -1;
for (int j = i; j < stations.size() &&
stations[j].pos <= current_position + current_water;
j++) {
if (stations[j].sup > max_supply) {
max_supply = stations[j].sup;
max_index = j;
}
}
if (max_index != -1) {
current_water += max_supply;
supply_count++;
i = max_index;
}
4. 两个返回的地方:
(1)每次循环完都需要判断当前的水是否足够到达终点,不能则继续下一次遍历
(2)当补给点遍历完后判断是否能够到达终点,不能则返回-1
if (current_water >= d - current_position) {
return supply_count;
} else {
return -1;
}
最终代码:
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
// 将补给站信息合并到一个结构体中,并按位置排序
struct Station {
int pos;
int sup;
};
int solution(int d, int w, std::vector<int> position, std::vector<int> supply) {
// Please write your code here
if (w >= d) {
return 0;
}
int current_position = 0;
int current_water = w;
int supply_count = 0;
vector<Station> stations;
for (int i = 0; i < position.size(); ++i) {
stations.push_back({position[i], supply[i]});
}
sort(stations.begin(), stations.end(),
[](const Station &a, const Station &b) { return a.pos < b.pos; });
for (int i = 0; i < stations.size(); i++) {
int distance_to_next = stations[i].pos - current_position;
// 如果当前水量不足以到达下一个补给站,返回 -1
if (current_water < distance_to_next) {
return -1;
}
current_position = stations[i].pos;
current_water -= distance_to_next;
// 选择最优的补给站进行补给
int max_supply = 0;
int max_index = -1;
for (int j = i; j < stations.size() &&
stations[j].pos <= current_position + current_water;
j++) {
if (stations[j].sup > max_supply) {
max_supply = stations[j].sup;
max_index = j;
}
}
if (max_index != -1) {
current_water += max_supply;
supply_count++;
i = max_index;
}
if (current_water >= d - current_position) {
return supply_count;
}
}
if (current_water >= d - current_position) {
return supply_count;
} else {
return -1;
}
}
int main() {
// You can add more test cases here
std::vector<int> testPosition = {170, 192, 196, 234, 261, 269, 291,
404, 1055, 1121, 1150, 1234, 1268, 1402,
1725, 1726, 1727, 1762, 1901, 1970};
std::vector<int> testSupply = {443, 185, 363, 392, 409, 358, 297,
70, 189, 106, 380, 130, 126, 411,
63, 186, 36, 347, 339, 50};
std::cout << (solution(10, 4, std::vector<int>{1, 4, 7},
std::vector<int>{6, 3, 5}))
<< std::endl;
std::cout << (solution(7, 23, std::vector<int>{5}, std::vector<int>{22}))
<< std::endl;
std::cout << (solution(2000, 200, testPosition, testSupply)) << std::endl;
return 0;
}
运行结果: