POJ 1328 Radar Insallation
阿新 • • 發佈:2020-07-03
由於poj不能使用c11,改在牛客網上OJ
題解
以每個島為中心,d為半徑畫個圓,交x軸為兩點,轉化為活動安排問題,即求最多有多少不覆蓋的線段。
程式碼
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; const double INF = 1e9; struct node { double s, e; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, d; while (cin >> n >> d, n != 0 && d != 0) { vector<node> record(n); bool noSolution = false; for (auto &i : record) { double t0, t1; cin >> t0 >> t1; double tmp = d * d - t1 * t1; if (tmp < 0) { noSolution = true; continue; } double t2 = sqrt(d * d - t1 * t1); i.s = t0 - t2, i.e = t0 + t2; } if (noSolution) { cout << -1 << '\n'; continue; } sort(record.begin(), record.end(), [](node &a, node &b) { return a.e < b.e; }); int cnt = 0; double lastEnd = -INF; for (auto &i : record) { if (i.s > lastEnd) { ++cnt; lastEnd = i.e; } } cout << cnt << '\n'; } return 0; }