题目大意:
有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆。求出最少需要的喷水装置个数。
Sample input
8 20 2
5 3
4 1
1 2
7 2
10 2
13 3
16 2
19 4
3 10 1
3 5
9 3
6 1
3 10 1
5 3
1 1
9 1
Sample output
6
2
-1
这题可以转化成区间覆盖问题,简单贪心
思路见码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
using namespace std;
#define LL long long
const int maxn=10005;
struct Pen{
double lb,rb;
bool operator < (const Pen& A) const {
return lb<A.lb;
}
}pens[maxn];
int main(){
//freopen("input.txt","r",stdin);
int n;
double l,w;
while(scanf("%d%lf%lf",&n,&l,&w)!=EOF){
for(int i=0;i<n;i++){
double p,r;
scanf("%lf%lf",&p,&r);
pens[i].lb=p-sqrt(r*r-(w*w)/4);
pens[i].rb=p+sqrt(r*r-(w*w)/4);
}
sort(pens,pens+n);
double lpos=0,rmax=0,newpos=0;
int ans=0,ifpossible=1;
for(int i=0;i<n;i++){
Pen& d=pens[i];
if(d.lb>lpos){
lpos=newpos;
if(d.lb>lpos){
ifpossible=0;
break;
}
ans++;
}
if(d.rb>rmax){
newpos=d.rb;
rmax=d.rb;
if(rmax>=l){
ans++;
break;
}
}
}
if(pens[0].lb>0||rmax<l) ifpossible=0;
if(ifpossible) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}