Wall(Graham算法)

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27110   Accepted: 9045

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. 
Wall(Graham算法)

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

9 100

200 400

300 400

300 300

400 300

400 400

500 400

500 200

350 200

200 200

Sample Output

1628

Hint

结果四舍五入就可以了
 
题意不解释了,它的目的就是形成一个凸包,再加上一个半径是L的圆的周长;
和poj2187类似,这个题WA到死的原因是用G++交输出用 %.0f 而不是 %.0lf, 看了discuss才知道......
 
 1 #include<stdio.h>

 2 #include<string.h>

 3 #include<algorithm>

 4 #include<math.h>

 5 using namespace std;

 6 

 7 const int maxn = 1100;

 8 const double PI = 3.1415926;

 9 int stack[maxn],top;

10 int n,l;

11 

12 struct Point

13 {

14     int x,y;

15 }points[maxn];

16 

17 int cmp(const Point &a, const Point &b)

18 {

19     if(a.y == b.y)

20         return a.x < b.x;

21     return a.y < b.y;

22 }

23 

24 int cross(const Point &p1,const Point &p2, const Point &p0)

25 {

26     return (p1.x-p0.x)*(p2.y-p0.y) - (p1.y-p0.y)*(p2.x-p0.x);

27 }

28 

29 double dis(const Point &a, const Point &b)

30 {

31     return sqrt((double)(a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));

32 }

33 

34 void Graham()

35 {

36     sort(points,points+n,cmp);

37     stack[0] = 0;

38     stack[1] = 1;

39     top = 1;

40 

41     for(int i = 2; i < n; i++)

42     {

43         while(top >= 1 && cross(points[i],points[stack[top]],points[stack[top-1]]) >= 0)

44             top--;

45         stack[++top] = i;

46     }

47 

48     stack[++top] = n-2;

49     int count = top;

50 

51     for(int i = n-3; i >= 0; i--)

52     {

53         while(top >= count && cross(points[i],points[stack[top]],points[stack[top-1]]) >= 0)

54             top--;

55         stack[++top] = i;

56     }

57 }

58 

59 int main()

60 {

61     scanf("%d %d",&n,&l);

62     for(int i = 0; i < n; i++)

63         scanf("%d %d",&points[i].x,&points[i].y);

64 

65     double ans = 2*PI*l;

66     Graham();

67     for(int i = 0; i < top; i++)

68     {

69         ans += dis(points[stack[i]],points[stack[i+1]]);

70     }

71     printf("%.0f\n",ans);

72     return 0;

73 }
View Code

 

 

你可能感兴趣的:(算法)