POJ 1328 贪心

ゞ 浴缸里的玫瑰 2021-10-15 11:51 381阅读 0赞

算法:

1.求出覆盖该岛的圆得区间, 將问题转换为求过出最少得点,保证每个区间至少有一个点。

2.按区间的左端排序

3.更新rad

ContractedBlock.gif ExpandedBlockStart.gif

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <algorithm>
  6. #include <math.h>
  7. using namespace std;
  8. struct node
  9. {
  10. double x, y;
  11. bool operator < (const node& A) const
  12. {
  13. return x < A.x;
  14. }
  15. }px[10000], seg[10000];
  16. double Circle1( double x, double y, double R)
  17. {
  18. return x + sqrt( R * R - y * y );
  19. }
  20. double Circle2( double x, double y, double R)
  21. {
  22. return x - sqrt( R * R - y * y );
  23. }
  24. int main( )
  25. {
  26. int n, d, ans = 1;
  27. while( scanf("%d%d", &n, &d), n + d)
  28. {
  29. int t = 0, num = 0;
  30. double ymax = 0;
  31. for( int i = 0; i < n; i++)
  32. {
  33. scanf("%lf%lf",&px[i].x, &px[i].y);
  34. if( px[i].y > ymax )
  35. ymax = px[i].y;
  36. }
  37. if( ymax > d )
  38. {
  39. printf("Case %d: %d\n", ans++, -1);
  40. continue;
  41. }
  42. //求出圆心区间【X,Y】
  43. for( int i = 0; i < n; i++)
  44. {
  45. double x1 = px[i].x;
  46. double y1 = px[i].y;
  47. double Y = Circle1( x1, y1, d);
  48. double X = Circle2( x1, y1, d);
  49. seg[t].x = X;
  50. seg[t].y = Y;
  51. t++;
  52. }
  53. sort(seg, seg + t );
  54. double rad = seg[0].y;
  55. num = 1;
  56. for( int i = 1; i < t; i++)
  57. {
  58. if( rad - seg[i].x > 10e-7 )
  59. {
  60. if( seg[i].y - rad < 10e-7 )
  61. rad = seg[i].y;
  62. }
  63. else if( seg[i].x - rad > 10e-7 )
  64. {
  65. rad = seg[i].y;
  66. num++;
  67. }
  68. }
  69. printf("Case %d: %d\n", ans++, num);
  70. }
  71. }

转载于:https://www.cnblogs.com/tangcong/archive/2012/07/09/2582315.html

发表评论

表情:
评论列表 (有 0 条评论,381人围观)

还没有评论,来说两句吧...

相关阅读

    相关 POJ 1328, Radar Installation

    贪心算法的基本要素 1.贪心选择性质 所谓贪心选择性质是指所求问题的整体最优解可以通过一系列局部最优的选择,即贪心选择来达到。这是贪心算法可行的第一个基本要素,也是贪心