题目传送门
这题有点水。
前言
这道题要求我们选择最少数量的路灯来照亮整个高速公路。我们需要确保每个点都被至少一盏灯覆盖,同时尽量减少安装的灯泡数量。
思路
这道题可以采用贪心算法来解决。我们从高速公路起点开始,每次选择能覆盖当前未照亮区域的最远路灯,确保每次安装的灯泡能覆盖尽可能长的路段,直到整个高速公路被完全照亮或确定无法实现全路段照明。
代码
#include <bits/stdc++.h>
using namespace std;
void sv() {
int T;
cin >> T;
for (int t = 1; t <= T; ++t) {
int M, R, N;
cin >> M >> R >> N;
vector<int> X(N);
for (int i = 0; i < N; ++i) {
cin >> X[i];
}
int r = 0;
int p = 0;
int i = 0;
bool pp = true;
while (p < M && pp) {
int f = -1;
while (i < N && X[i] - R <= p) {
f = X[i];
++i;
}
if (f == -1) {
pp = false;
break;
}
p = f + R;
++r;
}
if (pp && p >= M) {
cout << "Case #" << t << ": " << r << endl;
} else {
cout << "Case #" << t << ": IMPOSSIBLE" << endl;
}
}
}
int main() {
sv();
return 0;
}