uva 10382题解(贪心)
2015-05-04 22:50
218 查看
题目大意:
有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆。求出最少需要的喷水装置个数。
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
2
-1
这题可以转化成区间覆盖问题,简单贪心
思路见码
有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆。求出最少需要的喷水装置个数。
Sample input
8 20 25 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
62
-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; }
相关文章推荐
- UVA 10382 - Watering Grass 贪心
- UVa 10382 喷水装置(贪心)
- UVA 10382 贪心(区间覆盖)
- Uva 10382 贪心
- uva 10382 Watering Grass(贪心-区间覆盖变形)
- UVA 10382 Watering Grass (贪心 + 区间覆盖问题)
- UVa 10382 - Watering Grass(贪心+区间覆盖问题)
- uva 10382区间覆盖贪心
- 10382 - Watering Grass uva贪心
- 《黑书》uva10382喷水装置,贪心经典题
- uva 10382 Watering Grass_贪心
- UVA 10382 Watering Grass(贪心 区间覆盖问题)
- UVA - 10382 Watering Grass(贪心)
- uva 10382 Watering Grass_贪心
- UVA - 10382 (区间贪心)
- uva 10382 - Watering Grass(贪心)
- UVA - 10382 Watering Grass 贪心
- UVA - 10382 Watering Grass (贪心+区间覆盖)
- UVa 10382 - Watering Grass (贪心—区间覆盖)
- uva 10382 Watering Grass(贪心-区间覆盖变形)