POJ-1113 wall(凸包)

淩亂°似流年 2022-04-24 10:32 333阅读 0赞

题目衔接:http://poj.org/problem?id=1113

Wall














Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 42309   Accepted: 14432

Description

Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King’s castle. The King was so greedy, that he would not listen to his Architect’s proposals to build a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall.

1113_1.gif

Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King’s requirements.

The task is somewhat simplified by the fact, that the King’s castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle’s vertices in feet.

Input

The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King’s castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to the castle.

Next N lines describe coordinates of castle’s vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.

Output

Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King’s requirements. You must present the integer number of feet to the King, because the floating numbers are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.

Sample Input

  1. 9 100
  2. 200 400
  3. 300 400
  4. 300 300
  5. 400 300
  6. 400 400
  7. 500 400
  8. 500 200
  9. 350 200
  10. 200 200

Sample Output

  1. 1628

Hint

结果四舍五入就可以了

题目大意:给你一个城堡让你用最小的花费将其包起来,且墙离城堡最短距离为l

思路:凸包。。先计算出凸包距离,最后加上四个半圆就好了(半径为l)即一个整圆。

最后注意用c++交

  1. /*
  2. 题目大意:给你一个城堡让你用最小的花费将其包起来,且墙离城堡最短距离为l
  3. 思路:凸包,先计算出直线上距离,最后加上四个半圆就好了
  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. ///距离
  35. double dis(point a,point b)
  36. {
  37. return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
  38. }
  39. ///叉积
  40. int mulit(point a,point b,point c)
  41. {
  42. return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
  43. }
  44. ///极角排序:按照该点与x轴的夹角进行逆时针比较
  45. bool cmp(point a,point b)
  46. {
  47. int temp=mulit(p[0],a,b);
  48. if(temp>0)
  49. return true;
  50. if(temp==0&&dis(p[0],a)<dis(p[0],b))
  51. return true;
  52. return false;
  53. }
  54. ///凸包
  55. void tb(int n)
  56. {
  57. point p0;
  58. int k;
  59. p0=p[0];
  60. for(int i=1; i<n; i++)
  61. {
  62. if(p0.y>p[i].y||(p0.y==p[i].y&&p0.x>p[i].x))
  63. {
  64. p0=p[i];
  65. k=i;
  66. }
  67. }
  68. p[k]=p[0];
  69. p[0]=p0;
  70. sort(p+1,p+n,cmp);
  71. if(n==1)
  72. {
  73. top=0;
  74. Stack[0]=0;
  75. return ;
  76. }
  77. if(n==2)
  78. {
  79. top=1;
  80. Stack[0]=0;
  81. Stack[1]=1;
  82. return ;
  83. }
  84. if(n>2)
  85. {
  86. for(int i=0; i<=1; i++)
  87. Stack[i]=i;
  88. top=1;
  89. for(int i=2; i<n; i++)
  90. {
  91. while(top>0&&mulit(p[Stack[top-1]],p[Stack[top]],p[i])<=0)
  92. top--;
  93. top++;
  94. Stack[top]=i;
  95. }
  96. }
  97. }
  98. int main()
  99. {
  100. int n,r;
  101. while(scanf("%d%d",&n,&r)!=EOF)
  102. {
  103. for(int i=0; i<n; i++)
  104. cin>>p[i].x>>p[i].y;
  105. tb(n);
  106. double ans=0;
  107. for(int i=0; i<top; i++)
  108. ans+=dis(p[Stack[i]],p[Stack[i+1]]);
  109. ans+=dis(p[Stack[0]],p[Stack[top]]);
  110. ans+=pi*(2*r);
  111. printf("%.0f\n",ans);
  112. }
  113. return 0;
  114. }

发表评论

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

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

相关阅读