CODEFORCES #339 div2 C

C. Peter and Snow Blower

Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.

Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.

Peter decided to tie his car to point P and now he is wondering what is the area of ​​the region that will be cleared from snow. Help him.

Input

The first line of the input contains three integers — the number of vertices of the polygon n (), and coordinates of point P.

Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line.

All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.

Output

Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

input

3 0 0
0 1
-1 2
1 2

output

12.566370614359172464

input

4 1 -1
0 0
1 2
2 0
1 1

output

21.991148575128551812

簡單來說,記給定點到多邊形最遠距離爲x1,最近距離爲x2.求以給定點爲圓心,分別以x1,x2爲半徑的圓的面積的差值。一開始寫的時候直接對多邊形每個頂點考慮,最後WA的妥妥的。後來想到點到多邊形的最近距離不一定是到多邊形上某頂點的距離。

後來考察沒相鄰的兩個點與給定點構成的三角形,記多邊形上構成該三角形的兩個點分別爲p1,p2.若在以p1,p2爲頂點的角中存在鈍角,說明給定點到p1p2的最近距離爲到p1,p2中某個點的距離,否則就是用等面積法來求解最短距離。這裏使用叉乘來計算面積。

代碼:

<span style="font-size:14px;">#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;


struct Point
{
    double x,y;
}pt[100005],p;

const double PI=atan(1.0)*4.0;

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

double cross(Point p,Point p1,Point p2)  //求叉乘
{
    double x1=p1.x-p.x , y1=p1.y-p.y;
    double x2=p2.x-p.x , y2=p2.y-p.y;
    return (x1*y2-y1*x2);
}

double pm(Point p,Point p1,Point p2)
{
    double x1=p.x-p1.x , y1=p.y-p1.y;
    double x2=p2.x-p1.x , y2=p2.y-p1.y;
    return (x1*x2+y1*y2);
}

double solve(Point p,Point p1,Point p2)
{
    if (pm(p,p1,p2)*pm(p,p2,p1)<=0)
    {
        double tmp1=dis(p,p1);
        double tmp2=dis(p,p2);
        return min(tmp1,tmp2);
    }
    return fabs(cross(p,p1,p2))/dis(p1,p2);
}

int main()
{
    int n;
    while (scanf("%d%lf%lf",&n,&p.x,&p.y)==3)
    {
        double maxd=0,mind=1e30;
        double tmp;
        double ans;

        for (int i=0;i<n;i++)
        {
            scanf("%lf%lf",&pt[i].x,&pt[i].y);
        }

        pt[n]=pt[0];
        for (int i=0;i<n;i++)
        {
            tmp=dis(p,pt[i]);
            if (tmp>maxd)
            {
                maxd=tmp;
            }
            tmp=solve(p,pt[i],pt[i+1]);
            if (tmp<mind)
            {
                mind=tmp;
            }
        }
        ans=PI*(maxd*maxd-mind*mind);
        printf("%.20f\n",ans);
    }
    return 0;
}</span>

值得留意的是,題目中說輸入數據都是整數,然而並不能用整型來存儲,不然對強一些的數據,就會丟很多東西,所以還是用double來存儲讀入的數據。(年輕的博主在20組數據卡了半天)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章