T1 P3737 [HAOI2014]遥感监测
传送门:洛谷P3737
我们可以根据勾股定理求出每一个点在坐标轴上能覆盖的范围,
例如一个点 \(P(xi, yi)\) ,半径长 \(r\) 那么它在坐标轴上的覆盖范围就是:
\([xi-\sqrt{r^2-yi^2},xi+\sqrt{r^2-yi^2}]\);
对每个区间覆盖之后,我们开始思考如何贪心,对右端点进行排序,那么所有左端点小于等于该右端点的都可以跟这个右端点公用一个遥感;
贴代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
struct Node {
double l, r;
} e[maxn];
int n, r, ans, x[maxn], y[maxn];
bool cmp(Node a, Node b) {
return a.r < b.r;
}
void read() {
scanf("%d%d", &n, &r);
for (int i = 1; i <= n; ++ i) {
scanf("%d%d", &x[i], &y[i]);
double z = (double)sqrt(r * r - y[i] * y[i]);
e[i].l = (double)x[i] * 1.0 - z;
e[i].r = e[i].l + 2.0 * z;
}
sort(e + 1, e + 1 + n, cmp);
double j = e[1].r;
ans ++;
for (int i = 2; i <= n; ++ i) {
if (e[i].l > j) ans ++, j = e[i].r;
}
printf("%d\n", ans);
}
int main() {
read();
return 0;
}
标签:Node,xi,int,18,2023.04,T1,maxn,端点,ans
From: https://www.cnblogs.com/florence25/p/17329159.html