A - Wall解题报告(来自网络)

来源:互联网 发布:阿里云优惠口令看不懂 编辑:程序博客网 时间:2024/04/27 17:46

A - Wall
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 1113

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.

Sample Input

9 100200 400300 400300 300400 300400 400500 400500 200350 200200 200

Sample Output

1628

Hint

结果四舍五入就可以了

解题思路:先用Graham找出凸包,并算出凸包的周长再加上一个以L为半径的圆的周长就是所求的答案,因为凸多边形的内角和为(n-2)*180,n*360-(2*90)*n-(n-2)*180=360,刚好是一个完整的圆。

代码

#include<stdio.h>#include<math.h>#include<stdlib.h>#define PI 3.14159265358979#define exchange(a, b) {point t;t=a; a=b; b=t;}const int maxN = 1000;typedef struct {long x;long y;}point;int N, L, top=0;point pl[maxN+1];double direction(point p1, point p2, point p3, point p4) {return (p2.x-p1.x)*(p4.y-p3.y)-(p4.x-p3.x)*(p2.y-p1.y);}double dis(point p1, point p2) {return sqrt(1.0*(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));}int cmp(const void * arg1, const void * arg2) {point *a, *b;double d, dis1, dis2;a = (point *)arg1;b = (point *)arg2;d = direction(pl[1], *b, pl[1], *a);dis1 = dis(pl[1], *a);dis2 = dis(pl[1], *b);if( d!=0 ) {   return (int)d;}else if(dis1 > dis2){   return 1;}else if(dis1 <dis2 ) {   return -1;}else {   return 0;}}void push(point stack[], point p) {top++;stack[top] = p;}point pop(point stack[]) {top--;return stack[top+1];}int Graham_scan(point stack[]) {int i;push(stack, pl[1]);push(stack, pl[2]);for ( i=3; i<=N; i++ ) {   while(top>=2 && direction(stack[top-1], stack[top], stack[top], pl[i])<=0 ) {    pop(stack);   }   push(stack, pl[i]);}return top;}int main() {int i, num;point stack[maxN+1];double d=0;freopen("input.in", "r", stdin);scanf("%d%d",&N, &L);for ( i=1; i<=N; i++ ) {   scanf("%d%d", &pl[i].x, &pl[i].y);   if(i>1 && (pl[i].y<pl[1].y || (pl[i].y==pl[1].y && pl[i].x<pl[1].x))) {    exchange(pl[i], pl[1]);   }}qsort((void *)(pl+2), N-1, sizeof(point), cmp);num = Graham_scan(stack);for ( i=1; i<num; i++ ) {    //计算凸多边形的边长   d += dis(stack[i], stack[i+1]);}d += dis(stack[1], stack[num]);d += (2*PI*L); //加上圆的周长printf("%.f\n", d);return 0;}




原创粉丝点击