A - Wall解题报告(来自网络)
2012-05-09 07:22
246 查看
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
Sample Output
Hint
结果四舍五入就可以了
解题思路:先用Graham找出凸包,并算出凸包的周长再加上一个以L为半径的圆的周长就是所求的答案,因为凸多边形的内角和为(n-2)*180,n*360-(2*90)*n-(n-2)*180=360,刚好是一个完整的圆。
代码
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 100 200 400 300 400 300 300 400 300 400 400 500 400 500 200 350 200 200 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; }
相关文章推荐
- F - Dividing解题报告(来自网络)
- I - An Easy Problem!解题报告(来自网络)
- C - How I Mathematician Wonder What You Are!解题报告(来自网络)
- B - Run Away解题报告(来自网络)
- E - Pipe解题报告(来自网络)
- F - Summits解题报告(来自网络)
- G - Islands and Bridges解题报告(来自网络)
- C - Binary Stirling Numbers解题报告(来自网络)
- D - Magic Bracelet解题报告(来自网络)
- N - TOYS解题报告(来自网络)
- E - Biorhythms解题报告(来自网络)
- NOIP2014 无线网络发射器选址 解题报告(水题)
- 第三届华中区程序设计邀请赛暨武汉大学第十二届校赛 网络预选赛 水题解题报告
- codevs 3336 电话网络 SPFA 解题报告
- 华中农业大学第五届程序设计大赛网络同步赛解题报告(转)
- timus 1185. Wall URAL 解题报告 凸包
- [ZJOI2012]网络 解题报告
- 2015 亚洲区域赛长春赛区网络赛解题报告(9/13)
- 2014 ACM/ICPC 北京赛区网络赛解题报告汇总
- 华中农业大学第五届程序设计大赛网络同步赛解题报告2(转)