Test instructions: There is a piece of grass that is long and W, and has n water jets on its centerline, each of which can be sprayed with a circle with a radius of R at the center of P,
Choose as few devices as possible to moisten all the meadows.
Analysis: I go ah, do really disgusting, looks very simple, actually has n many pits Ah, first this question, should be able to see is greedy algorithm,
Specifically, the issue of interval coverage, the problem is generally not difficult, but there is a huge number of pits. Be aware of the following points:
1. This is a lawn, not a line, first you have to convert the laboratory into a line segment.
2. This water spray device is a circle, not a rectangle, to use mathematical knowledge to operate.
3. Twice times the radius of the input if it is less than or equal to the width, it should be ignored. Because you have no effect on the count.
4. This is to use floating point number, I did not consider the accuracy, at least also past, not too pit.
This picture is the address I cut from the Internet is http://blog.csdn.net/shuangde800/article/details/7828675
This picture can help to understand. The rest is not much different from the normal range coverage.
The code is as follows:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 10000 + 10;
struct node{
double l, r;
node() { }
node(double ll, double rr) : l(ll), r(rr) { }
bool operator < (const node &p) const{
return l < p.l;
}
};
node a[maxn];
int main(){
// freopen("in.txt", "r", stdin);
int n;
double l, w;
while(~scanf("%d %lf %lf", &n, &l, &w)){
int indx = 0;
double p, r;
for(int i = 0; i < n; ++i){
scanf("%lf %lf", &p, &r);
double x = sqrt(r*r - (w/2.0)*(w/2.0));
if(2 * r > w) a[indx++] = node(p-x, p+x);
}
sort(a, a+indx);
double s = 0, e = 0;
int cnt = 1;
if(a[0].l > s){ printf("-1\n"); continue; }
for(int i = 0; i < indx; ++i){
if(a[i].l <= s) e = max(e, a[i].r); //寻找最大区间
else{
++cnt; //记数
s = e; //更新s
if(a[i].l <= s) e = max(e, a[i].r);
else break; //无解,退出循环
}
if(e >= l) break; //提前结束
}
if(e < l) printf("-1\n");
else printf("%d\n", cnt);
}
return 0;
}
UVa 10382 Watering Grass (interval-covered greedy problem + math)