Time Limit: 2 second(s) | Memory Limit: 32 MB |
I have a land consisting of n trees. Since the trees are favorites to cows, I have a big problem saving them. So, I have planned to make a fence around the trees. I want the fence to be convex (curves are allowed) and the minimum distance from any tree to the fence is at least d units. And definitely I want a single big fence that covers all trees.
You are given all the information of the trees, to be specific, the land is shown as a 2D plane and the trees are plotted as 2D points. You have to find the perimeter of the fence that I need to create as described above. And you have to minimize the perimeter.
One tree, a circular fence is needed Two trees, the fence is shown
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing two integers n (1 ≤ n ≤ 50000), d (1 ≤ d ≤ 1000). Each of the next lines contains two integers xi yi (-108 ≤ xi, yi ≤ 108) denoting a position of a tree. You can assume that all the positions are distinct.
Output
For each case, print the case number and the minimum possible perimeter of the fence. Errors less than 10-3 will be ignored.
Sample Input | Output for Sample Input |
3 1 2 0 0 2 1 0 -1 0 2 3 5 0 0 5 0 0 5 | Case 1: 12.566370614 Case 2: 12.2831853 Case 3: 48.4869943478 |
Note
Dataset is huge, use faster i/o methods.
解题思路:求出凸包的周长+以d为半径的圆的周长
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-8
#define PI acos(-1.0)
using namespace std;
struct point{
double x,y;
}A[50010],result[50010];
int sig(double n){
if(fabs(n)<eps)return 0;
else if(n<0)return -1;
return 1;
}
double cp(point p1,point p2,point p3){
return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.x);
}
double dist(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(point a,point b){
double ans=cp(A[0],a,b);
if(!sig(ans))
return dist(A[0],a)-dist(A[0],b)<=0;
return ans>0;
}
int main()
{
int t,i,j,k=1,n,d;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&d);
int pos=0;
for(i=0;i<n;++i){
scanf("%lf%lf",&A[i].x,&A[i].y);
if(A[pos].y>=A[i].y){
if(A[pos].y==A[i].y){
if(A[pos].x>A[i].x)pos=i;
}
else pos=i;
}
}
point temp=A[pos];A[pos]=A[0];A[0]=temp;
sort(A+1,A+n,cmp);
int top=1;
result[0]=A[0];
result[1]=A[1];
for(i=2;i<n;++i){
while(top>0&&cp(result[top-1],result[top],A[i])<0)top--;
result[++top]=A[i];
}
double l=0;
for(i=0;i<=top;++i){
l+=dist(result[i],result[(i+1)%(top+1)]);
}
printf("Case %d: %.7lf\n",k++,l+2.0*PI*d);
}
return 0;
}