题目大意:有一个长为L,宽为M的矩形,矩形上面有K喷水器,喷水器的圆心都和矩形宽的中心在同一条直线上,求能否使用最少的喷水器使整个矩形都被水覆盖到
解题思路:区间覆盖问题,只是要考虑到宽。喷水器的喷水半径如果小于等于矩形宽的一半的话,就不用考虑了,因为那样无法使它所在的位置有水,如果相切的话,切点两边就喷不到水了。考虑到圆与上下两条长相交,连接两个交点,作一条线段,求出圆心到该线段的中点的长度就是喷水器谁能喷到的空间,这样就能形成一个矩形,宽就可以不用考虑了,借别人图一用:
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 10000 + 5;
struct Node {
double x;
double y;
friend bool operator <(const Node &a, const Node &b) {
return a.x < b.x;
}
};
Node node[maxn];
double l,w;
int n;
int main() {
double num1, num2;
double temp;
int count;
while(scanf("%d%lf%lf",&n,&l,&w) != EOF) {
count = 0;
for(int i = 0; i < n; i++) {
scanf("%lf%lf",&num1,&num2);
if(num2 <= w / 2)
continue;
else {
temp = sqrt(num2 * num2 - w * w / 4.0);
node[count].x = num1 - temp;
node[count].y = num1 + temp;
count++;
}
}
sort(node,node+count);
double left = 0,right = 0;
int i = 0,j,ans = 0;
bool flag = true;
while(i < count) {
j = i;
while(j < count && node[j].x <= left) {
if(right <= node[j].y)
right = node[j].y;
j++;
}
if(j == i)
break;
left = right;
ans++;
i = j;
if(left >= l) {
flag = false;
break;
}
}
if(flag)
printf("-1\n");
else
printf("%d\n", ans);
}
return 0;
}