A - Wall
Crawling in process...
Crawling failed
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
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
这道题目是要求我们在城堡的周围建一个距离它相同距离的围墙。我们首先要做的是先求出城堡的最小周长,然后再
加上一个圆的周长。要求城堡的最小周长就要用到graham扫描法来求出最短的周长,关于这种方法我还不是很懂,
所以就只能盗用模版来做了。
graham扫描法如图:
先找到所有点中最靠左下方的点P0,然后按照每个点与P0的极角(以P0为极点建立极坐标系)大小排序1-12,然后将P0、P1、P2入栈,然后扫描P3-P12,取栈头的两个点,与P3相比较,如果向量P1P3相对于P1P2是向左转的关系,那么将P3入栈,否则,将P2出栈,直到没有需要出栈的点的时候将下一点入栈,以此类推。(个人理解)
计算是否有向左转关系需要用向量叉积计算,最终遍历结束后,从P0开始输出栈内的所有点,就是所求凸包的逆时针顺序。
POJ1113就是一个最基础的凸包,下面是C++的代码,因为该题过于基础,此代码即可作为凸包grahamscan的基础模板:
#include <iostream> #include <cmath> using namespace std; /* PointSet[]:输入的点集 ch[]:输出的凸包上的点集,按照逆时针方向排列 n:PointSet中的点的数目 len:输出的凸包上的点的个数 */ struct Point { float x,y; }; //小于0,说明向量p0p1的极角大于p0p2的极角 float multiply(Point p1,Point p2,Point p0) { return((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y)); } float dis(Point p1,Point p2) { return(sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))); } void Graham_scan(Point PointSet[],Point ch[],int n,int &len) { int i,j,k=0,top=2; Point tmp; //找到最下且偏左的那个点 for(i=1;i<n;i++) if ((PointSet[i].y<PointSet[k].y)||((PointSet[i].y==PointSet[k].y)&&(PointSet[i].x<PointSet[k].x))) k=i; //将这个点指定为PointSet[0] tmp=PointSet[0]; PointSet[0]=PointSet[k]; PointSet[k]=tmp; //按极角从小到大,距离偏短进行排序 for (i=1;i<n-1;i++) { k=i; for (j=i+1;j<n;j++) if( (multiply(PointSet[j],PointSet[k],PointSet[0])>0) ||((multiply(PointSet[j],PointSet[k],PointSet[0])==0) &&(dis(PointSet[0],PointSet[j])<dis(PointSet[0],PointSet[k]))) ) k=j;//k保存极角最小的那个点,或者相同距离原点最近 tmp=PointSet[i]; PointSet[i]=PointSet[k]; PointSet[k]=tmp; } //第三个点先入栈 ch[0]=PointSet[0]; ch[1]=PointSet[1]; ch[2]=PointSet[2]; //判断与其余所有点的关系 for (i=3;i<n;i++) { //不满足向左转的关系,栈顶元素出栈 while(multiply(PointSet[i],ch[top],ch[top-1])>=0) top--; //当前点与栈内所有点满足向左关系,因此入栈. ch[++top]=PointSet[i]; } len=top+1; } const int maxN=1010; Point PointSet[maxN]; Point ch[maxN]; int n; int len; int main() { double ans=0; int d; cin>>n>>d; for(int i=0;i<n;i++) cin>>PointSet[i].x>>PointSet[i].y;//input the data; Graham_scan(PointSet,ch,n,len); for(int i=0;i<len;i++) ans+=dis(ch[i],ch[(i+1)%len]); ans+=2*d*acos(-1.0); //等价于圆形周长 cout<<(int)(ans+0.5)<<endl; //四舍五入 return 0; }