PKU 1031 Fence

Fence

真面目に計算すると大変そうだけど、原点から見て360度のうちどれだけ壁が見えるかを考えれば良い。ある点を基準として最左と最右の点の角度を求める。ただし角度は最大で2π。

#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

int main()
{
    const double PI = acos(-1.0);

    double k, h;  cin >> k >> h;
    int N;  cin >> N;

    double org, pt, tmin, tmax;

    for ( int i=0; i<=N; i++ )
    {
        double t;
        if ( i < N )
        {
            double x, y;  cin >> x >> y;
            t = atan2(y,x);
        }
        else
            t = org;

        if ( i == 0 )
            org = pt = tmin = tmax = t;
        
        t = fmod( t-pt+5*PI, 2*PI ) - PI + pt;
        tmin = min( tmin, t );
        tmax = max( tmax, t );
        pt = t;
    }

    cout.setf( ios::fixed );
    cout.precision( 2 );
    cout << min(tmax-tmin,2*PI) * k * h << endl;
}