This commit is contained in:
2026-07-28 00:33:27 +08:00
parent 2b6dc69bde
commit 8bc1b22a1c
14 changed files with 529 additions and 124 deletions
+59
View File
@@ -0,0 +1,59 @@
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
double other_leg(const double leg, const double diag) {
return sqrt(diag * diag - leg * leg);
}
double diag(const double leg1, const double leg2) {
return sqrt(leg1 * leg1 + leg2 * leg2);
}
vector<pair<double, double>> target_ranges(vector<pair<int, int>> islands, const int distance) {
vector<pair<double, double>> ranges;
for (auto& [island_x, island_y] : islands) {
if (island_y > distance) { throw std::runtime_error("Island is too far away from the radar."); }
const double offset = other_leg(island_y, distance);
ranges.emplace_back(island_x - offset, island_x + offset);
}
sort(ranges.begin(), ranges.end(),
[](const auto& a, const auto& b) { return a.second < b.second; });
return ranges;
}
int required_radars(const vector<pair<double, double>>& ranges) {
if (ranges.empty()) { return 0; }
int answer = 1;
double current_max_x = ranges[0].second;
for (const auto& [left,right] : ranges) {
if (left > current_max_x) { answer++, current_max_x = right; } // 放不下,加雷达
else if (right < current_max_x) { current_max_x = right; } // 放下,需要左移雷达
clog << "current_max_x: " << current_max_x << " answer: " << answer << endl;
}
return answer;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, distance, case_id = 0;
while (cin >> n >> distance) {
if (n == 0 && distance == 0) { break; }
vector<pair<int, int>> islands(n);
for (int i = 0; i < n; i++) {
cin >> islands[i].first >> islands[i].second;
}
// cout << "Case " << ++case_id << ": ";
try {
cout << required_radars(target_ranges(islands, distance)) << endl;
} catch (std::runtime_error&) {
cout << -1 << endl;
}
}
}