喷水装置(二)
-
描述
-
有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿。请在给出的喷水装置中选择尽量少的喷水装置,把整个草坪全部润湿。
-
输入
-
第一行输入一个正整数N表示共有n次测试数据。
每一组测试数据的第一行有三个整数n,w,h,n表示共有n个喷水装置,w表示草坪的横向长度,h表示草坪的纵向长度。
随后的n行,都有两个整数xi和ri,xi表示第i个喷水装置的的横坐标(最左边为0),ri表示该喷水装置能覆盖的圆的半径。
输出
-
每组测试数据输出一个正整数,表示共需要多少个喷水装置,每个输出单独占一行。
如果不存在一种能够把整个草坪湿润的方案,请输出0。
样例输入
-
2 2 8 6 1 1 4 5 2 10 6 4 5 6 5
样例输出
-
1 2
-
第一行输入一个正整数N表示共有n次测试数据。
想法:首先我们把喷水装置分为二类
一类:它不能使纵向长h草坪全部润湿,开始位置=和结束位置为ws。
二类:它能使纵向长h草坪全部润湿,它的开始位置为ws-sqrt(ks*ks-(1.0*h*h)/4);结束位置为ws+sqrt(ks*ks-(1.0*h*h)/4)。
再按开始位置排序,再用for遍历,选开始位置小于上次结束位置,并它的结束位置尽可能大
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
struct section
{
double a;
double b;
}sec[10010];
int cmp(const void *x,const void *y)
{
if( ((section *)x)->a > ((section *)y)->a )
return 1;
else
return -1;
}
int main()
{
int N;
scanf("%d",&N);
while(N--)
{
memset(sec,0,sizeof(sec));
int n,w,h;
int i,j;
scanf("%d %d %d",&n,&w,&h);
int ws,ks;
for(i=0;i<n;i++)
{
scanf("%d %d",&ws,&ks);
if(2*ks<h)
{
sec[i].a=ws;
sec[i].b=ws;
}
else
{
sec[i].a=ws-sqrt(ks*ks-(1.0*h*h)/4);
sec[i].b=ws+sqrt(ks*ks-(1.0*h*h)/4);
}
}
qsort(sec,n,sizeof(sec[1]),cmp);
//for(i=0;i<n;i++)
// {
// printf("%lf %lf\n",sec[i].a,sec[i].b);
//}
int count=0;
int flag=1;
double cur=0;
while(cur<w)
{
double t=cur;
for(i=0;i<n&&cur>=sec[i].a;i++)
{
t=t>sec[i].b?t:sec[i].b;
}
if(cur==t)
{
flag=0;
break;
}
else
{
cur=t;
count++;
//printf("%lf\n",cur);
//printf("%d\n",count);
}
}
if(flag==1)
printf("%d\n",count);
else
printf("0\n");
}
return 0;
}