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.
53
41
12
72
102
133
162
194
3101
35
93
61
3101
53
11
91
2
-1
求最少的圆,覆盖整个矩形,要·全部覆盖,所以要计算出每个圆投影在这个大的矩形上的有效部分的矩形面积,圆可以等效为这个有效矩形,
然后就是贪心了,个数最少当然是假设当前覆盖的长度为m,则找下个矩形的起始点小于m,终止点最大的那个;
由于是实数精度问题稍微注意下
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
820253
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; }
相关文章推荐
- uva 10382 Watering Grass
- UVa:10382 Watering Grass
- uva 10382 Watering Grass_贪心
- UVA 10382 Watering Grass
- uva 10382 Watering Grass_贪心
- UVA 10382(p79)----Watering Grass
- UVa 10382 - Watering Grass(贪心算法)
- UVa 10382 Watering Grass (区间覆盖贪心问题+数学)
- UVA 10382 Watering Grass (贪心 + 区间覆盖问题)
- Uva 10382 - Watering Grass
- UVa 10382 Watering Grass
- UVa 10382 - Watering Grass(贪心算法,区间覆盖问题)
- UVA - 10382 Watering Grass (区间覆盖 贪心)
- Uva-10382-Watering Grass
- uva 10382 - Watering Grass(区域覆盖问题)
- UVA 10382 Watering Grass
- ecjtu-summer training #5 E - Watering Grass UVA - 10382
- UVA - 10382 Watering Grass 贪心(几何)
- UVA 10382 Watering Grass
- Uva 10382 Watering Grass