CodeForces 614C Peter and Snow Blower

简单计算几何,只要算出圆心到多边形上的最短距离和最长距离即可

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn=100000+10;
struct Point
{
    double x,y;
} p[maxn];
double a,b;
int n;

double GetPointDistance(Point p1, Point p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}

double GetNearestDistance(Point PA, Point PB, Point P3)
{
    double a,b,c;
    a=GetPointDistance(PB,P3);
    if(a<=0.00001) return 0.0f;
    b=GetPointDistance(PA,P3);
    if(b<=0.00001) return 0.0f;
    c=GetPointDistance(PA,PB);
    if(c<=0.00001) return a;
    if(a*a>=b*b+c*c) return b;
    if(b*b>=a*a+c*c) return a;
    double l=(a+b+c)/2;
    double s=sqrt(l*(l-a)*(l-b)*(l-c));
    return 2*s/c;
}

int main()
{
    double Max=-1;
    double Min=-1;
    scanf("%d%lf%lf",&n,&a,&b);
    Point T;
    T.x=a;
    T.y=b;
    for(int i=0; i<n; i++) scanf("%lf%lf",&p[i].x,&p[i].y);
    for(int i=0; i<n; i++)
    {
        double MinLen=GetNearestDistance(p[(i+1)%n],p[i],T);
        if(Min==-1) Min=MinLen;
        else Min=min(Min,MinLen);

        double Len1=sqrt((p[i].x-a)*(p[i].x-a)+(p[i].y-b)*(p[i].y-b));
        double Len2=sqrt((p[(i+1)%n].x-a)*(p[(i+1)%n].x-a)+(p[(i+1)%n].y-b)*(p[(i+1)%n].y-b));
        double MaxLen=max(Len1,Len2);
        if(Max==-1) Max=MaxLen;
        else Max=max(Max,MaxLen);
    }

    printf("%lf\n",3.1415926*(Max*Max-Min*Min));
    return 0;
}

 

posted @ 2016-01-19 12:31  Fighting_Heart  阅读(369)  评论(0编辑  收藏  举报