poj2187Beauty Contest(凸包)poj1113Wall

http://poj.org/problem?id=2187

求出形成凸包的各边 找出最大的

View Code
 1 #include <iostream>

 2 #include<cstdio>

 3 #include<string.h>

 4 #include<algorithm>

 5 using namespace std;

 6 struct node

 7 {

 8     int x,y;

 9 }q[50011];

10 int dis(int x1,int y1,int x2,int y2)

11 {

12     return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);

13 }

14 node st[50011];

15 bool cmp(node a,node b)

16 {

17     if((a.x-q[1].x)*(b.y-q[1].y)==(a.y-q[1].y)*(b.x-q[1].x))

18     return dis(a.x,a.y,q[1].x,q[1].y)<dis(b.x,b.y,q[1].x,q[1].y);

19     else

20     return (a.x-q[1].x)*(b.y-q[1].y)>(a.y-q[1].y)*(b.x-q[1].x);

21 }

22 int judge(node a,node b,node c)

23 {

24     if((a.x-b.x)*(c.y-a.y)-(c.x-a.x)*(a.y-b.y)>0)

25     return 1;

26     else

27     return 0;

28 }

29 int main()

30 {

31     int i,j,k =1,n,m,top =1;

32     node t;

33     scanf("%d",&n);

34     for(i = 1 ; i <= n ;i++)

35     {

36         scanf("%d%d",&q[i].x,&q[i].y);

37         if(q[i].y<q[k].y||(q[i].y==q[k].y&&q[i].x<q[k].x))

38         k = i;

39     }

40     if(k!=1)

41     {

42         t = q[1];

43         q[1] = q[k];

44         q[k] = t;

45     }

46     sort(q+2,q+n+1,cmp);

47     st[top++] = q[1];

48     st[top++] = q[2];

49     st[top++] = q[3];

50     for(i = 4; i <= n ;)

51     {

52         if(top<3||judge(st[top-1],st[top-2],q[i]))

53         {

54             st[top++] = q[i++];

55         }

56         else

57         {

58             top--;

59         }

60     }

61     int ma = 0;

62     st[top] = q[1];

63     for(i = 1 ; i < top ; i++)

64     {

65         for(j = i+1 ; j < top ; j++)

66         {

67             ma = max(ma,dis(st[i].x,st[i].y,st[j].x,st[j].y));

68         }

69     }

70     printf("%d\n",ma);

71     return 0;

72 }

http://poj.org/problem?id=1113

一样是凸包 这个总长比凸包的总长正好大了以L为半径的圆 这个看题上的那个图 很好想

直接拿上面的改一下 距离换成double 这里WA了好多次

View Code
 1 #include <iostream>

 2 #include<cstdio>

 3 #include<string.h>

 4 #include<algorithm>

 5 #include<cmath>

 6 #define PI 3.141592653

 7 using namespace std;

 8 struct node

 9 {

10     int x,y;

11 }q[50011];

12 double dis(int x1,int y1,int x2,int y2)

13 {

14     double x = x1-x2;

15     double y = y1-y2;

16     return sqrt(x*x+y*y);

17 }

18 node st[50011];

19 bool cmp(node a,node b)

20 {

21     if((a.x-q[1].x)*(b.y-q[1].y)==(a.y-q[1].y)*(b.x-q[1].x))

22     return dis(a.x,a.y,q[1].x,q[1].y)<dis(b.x,b.y,q[1].x,q[1].y);

23     else

24     return (a.x-q[1].x)*(b.y-q[1].y)>(a.y-q[1].y)*(b.x-q[1].x);

25 }

26 int judge(node a,node b,node c)

27 {

28     if((a.x-b.x)*(c.y-a.y)-(c.x-a.x)*(a.y-b.y)>0)

29     return 1;

30     else

31     return 0;

32 }

33 int main()

34 {

35     int i,j,k =1,n,m,top =1,l;

36     node t;

37     while(scanf("%d%d",&n,&l)!=EOF)

38     {

39         top = 1;

40        for(i = 1 ; i <= n ;i++)

41         {

42             scanf("%d%d",&q[i].x,&q[i].y);

43             if(q[i].y<q[k].y||(q[i].y==q[k].y&&q[i].x<q[k].x))

44             k = i;

45         }

46         if(k!=1)

47         {

48             t = q[1];

49             q[1] = q[k];

50             q[k] = t;

51         }

52         sort(q+2,q+n+1,cmp);

53         st[top++] = q[1];

54         st[top++] = q[2];

55         st[top++] = q[3];

56         for(i = 4; i <= n ;)

57         {

58             if(top<3||judge(st[top-1],st[top-2],q[i]))

59             {

60                 st[top++] = q[i++];

61             }

62             else

63             {

64                 top--;

65             }

66         }

67         st[top] = q[1];

68         double ma;

69         ma = 2*PI*l;

70         for(i = 1 ; i < top ; i++)

71         {

72             ma += dis(st[i].x,st[i].y,st[i+1].x,st[i+1].y);

73         }

74         printf("%.0f\n",ma);

75    }

76     return 0;

77 }

 

你可能感兴趣的:(test)