HDU-1392-Surround the Trees

r囧r小猫 2022-02-16 18:36 272阅读 0赞

题目衔接:http://acm.hdu.edu.cn/showproblem.php?pid=1392

Surround the Trees

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14038 Accepted Submission(s): 5428

Problem Description

There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.

1392-1.gif

There are no more than 100 trees.

Input

The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.

Zero at line for number of trees terminates the input for your program.

Output

The minimal length of the rope. The precision should be 10^-2.

样例输入输出:

9 12 7 24 9 30 5 41 9 80 7 50 87 22 9 45 1 50 7 0

243.06

题目大意:给你一个树林,问你最短需要多长的绳子能把整个树林包起来

思路:看一下图就感觉是凸包,我们可以使用Graham算法,扫描后计算长度(凸包试水)

注意两点:一:当只有一个点或两个点的时候要特判

二:算的时候不要忘记算最后一个点与第一个点的距离

代码:

  1. /*
  2. 题目大意:给你一个树林,问你最短需要多长的绳子能把整个树林包起来
  3. 思路:看一下图就感觉是凸包,我们可以使用Graham算法,扫描计算长度(凸包试水)
  4. */
  5. #include<map>
  6. #include<set>
  7. #include<stack>
  8. #include<queue>
  9. #include<cmath>
  10. #include<string>
  11. #include<cstdio>
  12. #include <vector>
  13. #include<cstring>
  14. #include<cstdlib>
  15. #include<iostream>
  16. #include<algorithm>
  17. using namespace std;
  18. #define ll unsigned long long
  19. #define inf 0x3f3f3f
  20. #define esp 1e-8
  21. #define bug {printf("mmp\n");}
  22. #define mm(a,b) memset(a,b,sizeof(a))
  23. #define T() int test,q=1;scanf("%d",&test); while(test--)
  24. #define Test() {freopen("F:\\test.in","r",stdin);freopen("F:\\test1.out","w",stdout);}
  25. const int maxn=2e4+10;
  26. const double pi=acos(-1.0);
  27. const int N=110;
  28. const ll mod=1e9+7;
  29. struct point
  30. {
  31. int x,y;
  32. } p[maxn];
  33. int Stack[maxn],top;
  34. int sign(double x)
  35. {
  36. if(fabs(x)<esp)
  37. return 0;
  38. if(x<0)
  39. return -1;
  40. else
  41. return 1;
  42. }
  43. ///距离
  44. double dis(point a,point b)
  45. {
  46. return sqrt((double)pow((a.x-b.x),2)+pow((a.y-b.y),2));
  47. }
  48. ///叉积
  49. int mulit(point a,point b,point c)
  50. {
  51. return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
  52. }
  53. ///极角排序:按照该点与x轴的夹角进行逆时针比较
  54. bool cmp(point a,point b)
  55. {
  56. int temp=mulit(p[0],a,b);
  57. if(temp>0)
  58. return true;
  59. if(temp==0&&dis(p[0],a)<dis(p[0],b))
  60. return true;
  61. return false;
  62. }
  63. ///凸包
  64. void tb(int n)
  65. {
  66. point p0;
  67. int k;
  68. p0=p[0];
  69. for(int i=1; i<n; i++)
  70. {
  71. if(p0.y>p[i].y||(p0.y==p[i].y&&p0.x>p[i].x))
  72. {
  73. p0=p[i];
  74. k=i;
  75. }
  76. }
  77. p[k]=p[0];
  78. p[0]=p0;
  79. sort(p+1,p+n,cmp);
  80. if(n==1)
  81. {
  82. top=0;
  83. Stack[0]=0;
  84. return ;
  85. }
  86. if(n==2)
  87. {
  88. top=1;
  89. Stack[0]=0;
  90. Stack[1]=1;
  91. return ;
  92. }
  93. if(n>2)
  94. {
  95. for(int i=0; i<=1; i++)
  96. Stack[i]=i;
  97. top=1;
  98. for(int i=2; i<n; i++)
  99. {
  100. while(top>0&&mulit(p[Stack[top-1]],p[Stack[top]],p[i])<=0)
  101. top--;
  102. top++;
  103. Stack[top]=i;
  104. }
  105. }
  106. }
  107. int main()
  108. {
  109. int n;
  110. while(scanf("%d",&n)&&n)
  111. {
  112. for(int i=0; i<n; i++)
  113. cin>>p[i].x>>p[i].y;
  114. if(n==1)
  115. {
  116. printf("0.00\n");
  117. continue;
  118. }
  119. if(n==2)
  120. {
  121. printf("%.2lf\n",dis(p[0],p[1]));
  122. continue;
  123. }
  124. tb(n);
  125. double ans=0;
  126. for(int i=0; i<top; i++)
  127. ans+=dis(p[Stack[i]],p[Stack[i+1]]);
  128. ans+=dis(p[Stack[0]],p[Stack[top]]);
  129. printf("%.2lf\n",ans);
  130. }
  131. }

发表评论

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

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

相关阅读

    相关 树形dp HDU6867 Tree

    [题目链接][Link 1] 多校,怎么越来越难了,是我变菜了吗。 不多说,这是一道树形dp的题目,AC代码如下。 AC代码 include <cstdio

    相关 HDU 5398 GCD Tree

    这题可以基本说是LCT的模板题目,几乎没什么多余的考虑。不像HDU 5333,那题除了用LCT维护最大生成树之外还有一些复杂的公式推算。 对于多组数据,从1枚举到n,然后加

    相关 HDU 6241 Color a Tree

    题意:给你一棵树,然后让你对树上的节点进行黑白染色。然后染色有一些要求,对于A类要求,要求在x的子树中,至少有y个节点被染成了黑色;对于B类要求,要求在树的所有节点除了x以及