Wall
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28157 | Accepted: 9401 |
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.
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
结果四舍五入就可以了
Source
题意:
求n个点的凸包的周长和圆的周长的和;
思路:
了解点的排序,左转判定;
判断向量p1=(x1,y1)到p2=(x2,y2)是否做左转,只需要判断x1*y2-x2*y1的正负,如果结果为正,则从p1到p2做左转。
1.将各点排序(请参看基础篇),为保证形成圈,把P0在次放在点表的尾部;
2.准备堆栈:建立堆栈S,栈指针设为t,将0、1、2三个点压入堆栈S;
3.对于下一个点i
只要S[t-1]、S[t]、i不做左转
就反复退栈;
将i压入堆栈S
4.堆栈中的点即为所求凸包;
代码:
#include "iostream" //poj 1113 #include "cstdio" #include "cmath" #include "cstring" #include "iomanip" #include "algorithm" using namespace std; const double eps = 1e-8; const double PI = acos(-1.0); int cmp(double x){ //判断一个实数的正负 if(fabs(x) < eps) return 0; if(x > 0) return 1; return -1; } inline double sqr(double x){ //计算一个数的平方 return x*x; } struct point{ double x,y; point(){}; point(double a,double b) : x(a),y(b) {} friend point operator - (const point &a,const point &b){ return point(a.x - b.x,a.y - b.y); } double norm(){ return sqrt(sqr(x) + sqr(y)); } }; double det(const point &a,const point &b){ //计算两个向量的叉积 return a.x*b.y - a.y*b.x; } double dist(const point &a,const point &b){ //计算两个点的距离 return (a-b).norm(); } #define N 1005 point start; point stacks[3*N]; bool cmp_sort(point a,point b) //对点进行极角排序(以左下角的点为参考点) { int temp = cmp(det(a-start,b-start));//(叉积判断) if(temp==1) return true; else if(temp==-1) return false; temp = cmp(dist(b,start)-dist(a,start)); //sort排序后只有true和false,牢记!!! if(temp==1) return true; return false; } void Melkman(point *a,int &n) //求n个点的凸包 { int k; //记录左下点的下标 int i,j; point temp; double minf = 100005; for(i=0; i<n; ++i) { if(cmp(a[i].x-minf) == -1) { minf = a[i].x; k = i; } else if(cmp(a[i].x-minf) == 0) { if(cmp(a[k].y-a[i].y) == 1) k = i; } } { //交换点 temp = a[0]; a[0] = a[k]; a[k] = temp; } start = a[0]; sort(a+1,a+n,cmp_sort); //排序! for(i=0; i<N; ++i) stacks[i].x = stacks[i].y = 0; stacks[0] = a[0]; stacks[1] = a[1]; int top=1; i = 2; while(i<n) { if(top==0 || cmp(det(stacks[top-1]-stacks[top],stacks[top]-a[i]))>=0) { top++; stacks[top] = a[i]; i++; } else top--; } for(n=0; n<=top; ++n) a[n] = stacks[n]; } int main() { int i,j; int n,r; point a[N]; while(scanf("%d %d",&n,&r)!=-1) { for(i=0; i<n; ++i) scanf("%lf %lf",&a[i].x,&a[i].y); Melkman(a,n); double ans = 2*PI*r; a[n] = a[0]; //将n个点连成一个圈 for(i=0; i<n; ++i) ans += dist(a[i],a[i+1]); printf("%.0lf ",ans); } return 0; }