Wall
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 38309 | Accepted: 13077 |
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个点的房子,要建最短的围墙围住它,且墙到房的距离要<=L。
思路:强房的最短距离,显然考虑房子最外面的点即可,就是一个凸包,距离<=L,画个图就发现加上2πL即可。
Code1
# include <iostream>
# include <cstdio>
# include <cmath>
# include <cstring>
# include <algorithm>
# define pi acos(-1.0)
using namespace std;
int n, l, tot=0;
struct node{double x, y;}a[1003], ans[1003];
inline node operator-(node a, node b)
{
return node{a.x-b.x, a.y-b.y};
}
inline double operator*(node a, node b)
{
return a.x*b.y - a.y*b.x;
}
inline double dis(node a)
{
return a.x*a.x+a.y*a.y;
}
inline bool cmp(node x, node y)
{
if(fabs((x-a[1])*(y-a[1]))>0) return (x-a[1])*(y-a[1])>0;
return dis(x-a[1])<dis(y-a[1]);
}
inline double cha(node a, node b, node c)
{
return fabs((a-b)*(a-c))/2;
}
int main()
{
scanf("%d%d",&n,&l);
for(int i=1; i<=n; ++i) scanf("%lf%lf",&a[i].x,&a[i].y);
for(int i=2;i<=n;i++)
if(a[i].y<a[1].y||(a[i].y==a[1].y&&a[i].x<a[1].x))
swap(a[1],a[i]);
sort(a+2,a+n+1,cmp);
int top=2;ans[1]=a[1];ans[2]=a[2];
for(int i=3; i<=n; ++i)
{
while(top>1&&(a[i]-ans[top-1])*(ans[top]-ans[top-1])>=0) --top;
ans[++top]=a[i];
}
ans[++top] = a[1];
double Ans=0;
for (int i=1;i<top;++i)
Ans += sqrt(dis(ans[i]-ans[i+1]));
Ans += 2*pi*l;
printf("%.0f\n",Ans);
return 0;
}
Code2
# include <iostream>
# include <cstdio>
# include <cmath>
# include <cstring>
# include <algorithm>
# define pi acos(-1.0)
using namespace std;
int n, l, ans[1003], tot=0;
inline double f(double x)
{
return x*x;
}
struct node
{
double x, y;
inline node operator-(const node &t)
{
node ret;
ret.x = x-t.x;
ret.y = y-t.y;
return ret;
}
inline node operator+(const node &t)
{
node ret;
ret.x = x+t.x;
ret.y = y+t.y;
return ret;
}
inline int det(const node &t)
{
return x*t.y-t.x*y;
}
inline double dist(node &t)
{
return sqrt(f(x-t.x)+f(y-t.y));
}
}a[1003];
bool cmp(const node &na, const node &nb)
{
if(na.x == nb.x) return na.y < nb.y;
return na.x < nb.x;
}
void solve()
{
sort(a, a+n, cmp);
int cnt=0, tmp;
for(int i=0; i<n; ++i)
{
while(cnt>1 && (a[ans[cnt-1]]-a[ans[cnt-2]]).det(a[i]-a[ans[cnt-1]]) <= 0) --cnt;
ans[cnt++] = i;
}
tmp = cnt;
for(int i=n-2; i>=0; --i)
{
while(cnt>tmp && (a[ans[cnt-1]]-a[ans[cnt-2]]).det(a[i]-a[ans[cnt-1]]) <= 0) --cnt;
ans[cnt++] = i;
}
tot = cnt;
}
int main()
{
scanf("%d%d",&n,&l);
for(int i=0; i<n; ++i) scanf("%lf%lf",&a[i].x,&a[i].y);
solve();
double Ans=0;
for (int i=0;i<tot-1;++i)
Ans+=a[ans[i]].dist(a[ans[i+1]]);
Ans += 2*pi*l;
printf("%.0f\n",Ans);
return 0;
}