当前位置: 代码迷 >> 综合 >> 凸包问题 nyoj 266 wall
  详细解决方案

凸包问题 nyoj 266 wall

热度:48   发布时间:2023-10-27 07:36:19.0

wall

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述
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.
输入
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.

There are multi test cases.EOF will terminate the input.
输出
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.
样例输入
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
样例输出
1628
上传者
苗栋栋


题意:一个贪心的国王,要求他的首席工程师给他城堡建立一个围墙,要求围墙离城堡最少距离为L而且要尽可能少的资源建造。为了精简问题,建筑师精准测量了城堡的各个顶点,求把这些顶点围起来并满足国王要求的最短墙长度。

看到这里应该知道是一个凸包问题,问题是如何确定墙的长度。

根据测试例子可以画出

可以清楚的看到L(墙)=凸包内点的距离和+以L为半径的圆的周长。至于精度问题,只需定义pi=3.141596就够了,因为最少不小与八英寸,相当于3/4英尺,大概就是0.7.

下面是代码:

#include<bits/stdc++.h>
using namespace std;
#define pi 3.1415926
struct point
{double x,y;
}p[3000],s[3000];//点
int cmp(point a,point b)
{if(a.x==b.x)return a.y<b.y;else  return a.x<b.x;
}
double distance_1(struct point a,struct point b)//两点之间距离公式
{return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}
bool judge(point a, point b, point c)//ab×ac叉乘
{int bax = b.x-a.x;int bay = b.y-a.y;int cax = c.x-a.x;int cay = c.y-a.y;if(bax*cay - bay*cax > 0)return true;return false;
}
int main()
{int m,l;while(~scanf("%d%d",&m,&l)){for(int i=0;i<m;i++)scanf("%lf %lf",&p[i].x,&p[i].y);sort(p,p+m,cmp);int top=0;for(int i=0;i<m;i++){while(top>=2&&!judge(s[top-2],s[top-1],p[i]))top--;s[top++]=p[i];}int t=top-1;for(int i=m-1;i>=0;i--){while(top>=t+2&&!judge(s[top-2],s[top-1],p[i]))top--;s[top++]=p[i];}--top;//上凸包和下凸包首尾重复double sum=0.0;for(int i=0;i<top;i++){if(i!=top-1){sum+=distance_1(s[i],s[i+1]);}else{sum+=distance_1(s[i],s[0]);}}//cout<<sum<<endl;sum+=2*pi*l;//加上周长printf("%.0lf\n",sum);}return 0;
}