Watering Grass (贪心,最小覆盖)
Posted fengzeng666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Watering Grass (贪心,最小覆盖)相关的知识,希望对你有一定的参考价值。
参考:
https://blog.csdn.net/shuangde800/article/details/7828675
https://www.cnblogs.com/haoabcd2010/p/6171794.html?utm_source=itdadao&utm_medium=referral
1 #include <iostream> 2 #include <stdio.h> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 7 8 using namespace std; 9 10 struct node 11 12 double L; 13 double R; 14 a[10010]; 15 16 bool cmp(node a, node b) 17 18 return a.R > b.R; 19 20 21 void sort_node(int n) 22 23 for (int i=0;i<n;i++) 24 25 int k=i; 26 for (int j=i+1;j<n;j++) 27 28 if (a[j].R>a[k].R) 29 k=j; 30 31 swap(a[k],a[i]); 32 33 34 35 int main() 36 37 int n; 38 double length, wide; 39 double center, radius; // c为圆心,r为半径 40 double t; 41 while(scanf("%d%lf%lf", &n, &length, &wide) != EOF) 42 43 for(int i = 0; i < n; ++i) 44 45 cin >> center >> radius; 46 t = sqrt(radius*radius - (wide/2.0)*(wide/2.0)); 47 a[i].L = center - t; 48 a[i].R = center + t; 49 50 51 //sort(a, a+n, cmp); 快排不知道为什么不能AC 52 sort_node(n); 53 54 double border = 0; 55 int cnt = 0; 56 while(border < length) 57 58 int i; 59 for(i = 0; i < n; ++i) 60 61 if(a[i].L <= border && a[i].R > border) 62 63 border = a[i].R; 64 cnt++; 65 break; 66 67 68 69 if(i == n) // 如果i==n,说明遍历完了这n个区间,仍然没有找到符合条件的,即不能完全覆盖 70 break; 71 72 73 if(border < length) 74 cout << -1 << endl; 75 else 76 cout << cnt << endl; 77 78 79 return 0; 80
以上是关于Watering Grass (贪心,最小覆盖)的主要内容,如果未能解决你的问题,请参考以下文章
UVA 10382 Watering Grass(区间覆盖,贪心)题解
UVa 10382 Watering Grass (区间覆盖贪心问题+数学)
UVA - 10382 Watering Grass(几何)
[UVA - 10382] Watering Grass 题解