hdu 1348 (凸包求周长)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348

Wall

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

Problem 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.
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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input

1

9 100

200 400

300 400

300 300

400 300

400 400

500 400

500 200

350 200

200 200

Sample Output

1628

----------------------------------------------------------------------

看了discuss,用了模板,然后做的这题

题目意思需要自己抽取出来,此题边界的四个角加起来是个圆,然后剩下的就是一个凸包的周长,然后就没什么了

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <math.h>
  5 #include <stdlib.h>
  6 #include <algorithm>
  7
  8 using namespace std;
  9
 10 const int MAX=1005;
 11 const double eps=1e-6;
 12
 13 typedef struct point
 14 {
 15     double x,y;
 16 }point;
 17
 18 point c[MAX];
 19 int top;
 20
 21 bool dy(double x,double y)
 22 {
 23     return x>y+eps;
 24 }
 25 bool xy(double x,double y)
 26 {
 27     return x<y-eps;
 28 }
 29 bool xyd(double x,double y)
 30 {
 31     return x<y+eps;
 32 }
 33 bool dyd(double x,double y)
 34 {
 35     return x>y-eps;
 36 }
 37 bool dd(double x,double y)
 38 {
 39     return fabs(x-y)<eps;
 40 }
 41
 42 double crossProduct(point a,point b,point c)
 43 {
 44     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
 45 }
 46 double dist(point a,point b)
 47 {
 48     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
 49 }
 50
 51 bool cmp(point a,point b)
 52 {
 53     if(dd(a.y,b.y))
 54     {
 55         return xy(a.x,b.x);
 56     }
 57     return xy(a.y,b.y);
 58 }
 59 bool cmp1(point a,point b)
 60 {
 61     double len=crossProduct(c[0],a,b);
 62     if(dd(len,0.0))
 63     {
 64         return xy(dist(c[0],a),dist(c[0],b));
 65     }
 66     return xy(len,0.0);
 67 }
 68
 69 point stk[MAX];
 70
 71 double dis()
 72 {
 73     double sum=0.0;
 74     for(int i=0;i<top;i++)
 75     {
 76         sum+=dist(stk[i],stk[i+1]);
 77     }
 78     sum+=dist(stk[top],stk[0]);
 79     return sum;
 80 }
 81
 82 double Graham(int n)
 83 {
 84     sort(c,c+n,cmp);
 85     sort(c+1,c+n,cmp1);
 86     top=0;
 87     stk[top++]=c[0];
 88     stk[top++]=c[1];
 89     stk[top++]=c[2];
 90     top--;
 91     for(int i=3;i<n;i++)
 92     {
 93         while(1)
 94         {
 95             point a,b;
 96             a=stk[top];
 97             b=stk[top-1];
 98             if(xyd(crossProduct(a,b,c[i]),0.0))
 99             {
100                 top--;
101             }
102             else
103                 break;
104         }
105         stk[++top]=c[i];
106     }
107     return dis();
108 }
109
110 int main()
111 {
112     int i,j,k,t;
113     int n,m;
114     int cas=0;
115     scanf("%d",&t);
116     while(t--)
117     {
118         scanf("%d%d",&n,&m);
119         for(i=0;i<n;i++)
120         {
121             scanf("%lf%lf",&c[i].x,&c[i].y);
122         }
123         if(cas++)
124         {
125             printf("\n");
126         }
127         double re=4*acos(0.0)*m;
128         double sum=Graham(n);
129         printf("%.0lf\n",sum+re);
130     }
131 }

hdu 1348 (凸包求周长)

时间: 07-23

hdu 1348 (凸包求周长)的相关文章

zoj 1453 Surround the Trees(凸包求周长)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=453 Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 r

hdu 1348(凸包)

Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4903    Accepted Submission(s): 1419 Problem Description Once upon a time there was a greedy King who ordered his chief Architect to build a

凸包求周长

1 #include <iostream> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstdio> 5 using namespace std; 6 int n; 7 typedef struct 8 { 9 double x; 10 double y; 11 }Point; 12 13 Point p[110],s[110]; 14 15 int top; 16 17 double Mul(P

(hdu 7.1.7)Wall(求凸包的周长——求将所有点围起来的最小凸多边形的周长)

题目: Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 119 Accepted Submission(s): 47   Problem Description Once upon a time there was a greedy King who ordered his chief Architect to build a wa

(hdu step 7.1.7)Wall(求凸包的周长——求将全部点围起来的最小凸多边形的周长)

题目: Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 119 Accepted Submission(s): 47   Problem Description Once upon a time there was a greedy King who ordered his chief Architect to build a wa

HDU 4667 Building Fence(求凸包的周长)

A - Building Fence Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Description Long long ago, there is a famous farmer named John. He owns a big farm and many cows. There are two kinds of cows on his farm, o

hdu 1348 Wall(凸包模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3386    Accepted Submission(s): 968 Problem Description Once upon a time there was a gre

POJ 1113 || HDU 1348: wall(凸包问题)

传送门: POJ:点击打开链接 HDU:点击打开链接 下面是POJ上的题: Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29121   Accepted: 9746 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's cast

HDU 1392 凸包

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10345    Accepted Submission(s): 4009 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to