您的位置:首页 > 其它

uva 10382 - Watering Grass

2012-09-05 21:29 381 查看
ProblemE

WateringGrass

Input:
standardinput

Output:standardoutput

TimeLimit:3seconds

nsprinklersareinstalledinahorizontalstripofgrass
lmeterslongandwmeterswide.Eachsprinklerisinstalledatthehorizontalcenterlineofthestrip.Foreachsprinklerwearegivenitspositionasthedistancefromtheleftendofthecenterlineanditsradiusofoperation.

Whatistheminimumnumberofsprinklerstoturnoninordertowatertheentirestripofgrass?



Input
Inputconsistsofanumberofcases.Thefirstlineforeachcasecontainsintegernumbers
n,landwwithn<=10000.Thenextnlinescontaintwointegersgivingthepositionofasprinkleranditsradiusofoperation.(Thepictureaboveillustratesthefirstcasefromthesampleinput.)

Output
Foreachtestcaseoutputtheminimumnumberofsprinklersneededtowatertheentirestripofgrass.Ifitisimpossibletowatertheentirestripoutput-1.

Sampleinput

8202
53
41
12
72
102
133
162
194
3101
35
93
61
3101
53
11
91

SampleOutput

6

2
-1
求最少的圆,覆盖整个矩形,要·全部覆盖,所以要计算出每个圆投影在这个大的矩形上的有效部分的矩形面积,圆可以等效为这个有效矩形,
然后就是贪心了,个数最少当然是假设当前覆盖的长度为m,则找下个矩形的起始点小于m,终止点最大的那个;
由于是实数精度问题稍微注意下
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<string.h>
usingnamespacestd;
structnode
{
doublebegin,end;
}a[100001];
intcmp(nodex,nodey)
{returnx.begin<y.begin;}
intmain()
{
ints,i,j,n,l,tail,t,f,max;
doubled,o,r,w;
while(scanf("%d%d%lf\n",&n,&l,&w)!=EOF)
{
s=0;
memset(a,0,sizeof(a));
while(n--)
{
scanf("%lf%lf",&o,&r);
if(2*r>=w)
{
d=sqrt(r*r-w*w/4);
a[s].begin=o-d;//计算左右边界
a[s].end=o+d;
if(a[s].begin<0)a[s].begin=0;
if(a[s].end>l)a[s].end=l;
++s;
}
}
sort(a,a+s,cmp);
i=0;tail=0;n=1;
if(a[i].begin==0)
{
while(i<s&&a[i].begin==0)
{
if(a[i].end>a[tail].end)tail=i;
++i;
}
t=tail;max=tail;
while(a[tail].end<l)
{
f=1;
while(t+1<s&&a[t+1].begin<=a[tail].end)
{
++t;f=0;
if(a[t].end>a[max].end)
max=t;
}
t=max;tail=max;++n;
if(f)gotothere;//感觉goto蛮方便的;
}
printf("%d\n",n);
}
else
{
there:;
printf("-1\n");
}
}
return0;
}







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: