Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3028 Accepted Submission(s):
861
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.
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.
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.
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
题意:要在城堡外面建立城墙,要求城堡任意点的距离与城墙的距离>=r,求所建城墙的周长最短。
思路:该题是凸包问题,墙的周长 = 凸包的周长 + 以r为半径的圆的周长。
思路:该题是凸包问题,墙的周长 = 凸包的周长 + 以r为半径的圆的周长。
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 struct Point{ 8 int x, y; 9 }; 10 Point p[1001], ch[1002]; 11 int Ver, Cir; 12 double Cross(Point A, Point B, Point C){ 13 return (A.x-C.x)*(B.y-C.y) - (A.y-C.y)*(B.x-C.x); 14 } 15 const double eps = 1e-10; 16 const double PI = acos(-1.0); 17 int Dcmp(double x){ 18 if(fabs(x) < eps)return 0; 19 else return x < 0 ? -1 : 1; 20 } 21 bool cmp(Point a, Point b){ 22 return a.x == b.x ? a.y < b.y : a.x < b.x; 23 } 24 25 double Length(Point A, Point B){ 26 return sqrt(1.0*(B.x-A.x)*(B.x-A.x) + (B.y-A.y)*(B.y-A.y)); 27 } 28 double ConvexHull() 29 { 30 sort(p, p + Ver, cmp);//先比较x坐标再比较y坐标 31 int m = 0; 32 for(int i = 0; i < Ver; i++){ 33 while( m > 1 && Dcmp(Cross(ch[m-1], p[i], ch[m-2])) <= 0 )m--; 34 ch[m++] = p[i]; 35 } 36 int k = m; 37 for(int i = Ver - 2; i >= 0; i--){ 38 while( m > k && Dcmp(Cross(ch[m-1], p[i], ch[m-2])) <= 0 )m--; 39 ch[m++] = p[i]; 40 } 41 if(Ver > 1)m--;//起始点重复 42 double sum = 0; 43 for(int i = 1; i < m; i++){ 44 sum += Length(ch[i], ch[i-1]); 45 } 46 sum += Length(ch[m-1], ch[0]); 47 return sum;//凸包的周长 48 } 49 int main() 50 { 51 int i, j, t; 52 cin>>t; 53 while(t--) 54 { 55 cin>>Ver>>Cir; 56 for(i = 0; i < Ver; i++){ 57 scanf("%d%d", &p[i].x, &p[i].y); 58 } 59 double len = ConvexHull(); 60 len += 2 * PI * Cir;//圆的周长 61 printf("%.0lf ", len); 62 if(t != 0)puts(""); 63 } 64 return 0; 65 }