Problem: Example

Input

4 4
0 0
6 0
6 6
0 6


Output

35.759506

Solution (in CPP):

Solution:
#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); }
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

using D = double;
const D PI = acos(D(-1)), EPS = 1e-10;

struct Pt2 {
    D x, y;
    Pt2() {}
    Pt2(D _x, D _y) : x(_x), y(_y) {}
    Pt2 operator+(const Pt2 &r) const { return Pt2(x+r.x, y+r.y); }
    Pt2 operator-(const Pt2 &r) const { return Pt2(x-r.x, y-r.y); }
    Pt2 operator*(const Pt2 &r) const { return Pt2(x*r.x-y*r.y, x*r.y+y*r.x); }

    Pt2 operator*(const D &r) const { return Pt2(x*r, y*r); }
    Pt2 operator/(const D &r) const { return Pt2(x/r, y/r); }

    Pt2& operator+=(const Pt2 &r) { return *this=*this+r; }
    Pt2& operator-=(const Pt2 &r) { return *this=*this-r; }
    Pt2& operator*=(const Pt2 &r) { return *this=*this*r; }
    Pt2 operator-() const { return Pt2(-x, -y); }

    D abs() const { return sqrt(x*x + y*y); }
    D rabs() const { return max(::abs(x), ::abs(y)); } // robust abs
    D arg() const { return atan2(y, x); }

    pair<D, D> to_pair() const { return make_pair(x, y); }
    static Pt2 polar(D le, D th) { return Pt2(le*cos(th), le*sin(th)); }
};
ostream& operator<<(ostream& os, const Pt2 &p) {
    os << "(" << p.x << ", " << p.y << ")";
    return os;
}
using P = Pt2;

struct L {
    P s, t;
    L() {}
    L(P _s, P _t) : s(_s), t(_t) {}
    P vec() const { return t-s; }
    D abs() const { return vec().abs(); }
    D arg() const { return vec().arg(); }
};

int sgn(D a) {
    if (abs(a) <= EPS) return 0;
    return (a < 0) ? -1 : 1;
}
int sgn(D a, D b) { return sgn(a-b); }
//relative sign
int rsgn(D a, D f) {
    if (abs(a) <= f*EPS) return 0;
    return (a < 0) ? -1 : 1;
}
bool near(P a, P b) { return !sgn((a-b).abs()); }

D cross(P a, P b) { return a.x*b.y - a.y*b.x; }
D dot(P a, P b) { return a.x*b.x + a.y*b.y; }

// -2, -1, 0, 1, 2 : front, clock, on, cclock, back
int ccw(P b, P c) {
    int s = rsgn(cross(b, c), b.rabs());
    if (s) return s;
    if (!sgn(c.rabs()) || !sgn((c-b).rabs())) return 0;
    if (dot(b, c) < 0) return 2;
    if (dot(-b, c-b) < 0) return -2;
    return 0;
}
int ccw(P a, P b, P c) { return ccw(b-a, c-a); }
int ccw(L l, P p) { return ccw(l.s, l.t, p); }


D distLP(const L &l, const P &p) {
    return abs(cross(l.vec(), p-l.s)) / l.abs();
}

D distSP(const L &s, const P &p) {
    P s2 = s.vec() * P(0, 1);
    if (ccw(s.s, s.s+s2, p) == 1) return (s.s-p).abs();
    if (ccw(s.t, s.t+s2, p) == -1) return (s.t-p).abs();
    return distLP(s, p);
}

int crossLL(const L &l, const L &m, P &r) {
    if (sgn(cross(l.vec(), m.vec())) == 0) {
        r = l.s;
        if (ccw(l.s, l.t, m.s) % 2 == 0) return -1;
        return 0;
    }
    D t = cross(l.vec(), l.t - m.s) / cross(l.vec(), m.vec());
    r = m.s + m.vec() * t;
    return 1;
}

using Pol = V<P>;

struct C {
    P p; D r;
    C() {}
    C(P p, D r) : p(p), r(r) {}
};

D insAreaTC(const P &a, const P &b, const D &r) {
    if (near(a, b)) return 0;
    D r2 = distSP(L(a, b), P(0, 0));
//    cout << "Z " << a << " " << b << " " << c.p << " " << c.r << endl;
    if (sgn(max(a.abs(), b.abs()), r) != 1) return cross(a, b) / 2;
    if (sgn(r, r2) != 1) {
        D ar = b.arg() - a.arg();
        ar = fmod(fmod(ar, 2*PI) + 2*PI, 2*PI);
        if (!sgn(ar - 2*PI)) ar = 0;
        if (ar >= PI) ar -= 2*PI;
        return r * r * ar / 2;
    }
    return insAreaTC(a, (a+b)/2, r) + insAreaTC((a+b)/2, b, r);
}

const P& c_at(const Pol &p, int idx) {
    int n = int(p.size());
    return p[idx < n ? idx : idx-n];
}
D insAreaPolC(const Pol &p, const C &c) {
    int n = int(p.size());
    D sm = 0;
    for (int i = 0; i < n; i++) {
        sm += insAreaTC(p[i] - c.p, c_at(p, i+1) - c.p, c.r);
    }
    return sm;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20);
    int n; D r;
    cin >> n >> r;
    Pol pol;

    D ymi = 1e100, yma = -1e100;
    for (int i = 0; i < n; i++) {
        D x, y;
        cin >> x >> y;
        pol.push_back(P(x, y));
        ymi = min(ymi, y);
        yma = max(yma, y);
    }

    auto calc = [&](D y) {
        D xmi = 1e100, xma = -1e100;
        for (int i = 0; i < n; i++) {
            P p;
            if (crossLL(L(pol[i], c_at(pol, i+1)), L(P(0, y), P(1, y)), p) == 0) continue;
            if (ccw(pol[i], c_at(pol, i+1), p) != 0) continue;
            xmi = min(xmi, p.x); xma = max(xma, p.x);
        }
        D lw = xmi, up = xma;
        for (int ph = 0; ph < 50; ph++) {
            D md1 = (lw+lw+up) / 3;
            D md2 = (lw+up+up) / 3;
            D z1 = insAreaPolC(pol, C(P(md1, y), r));
            D z2 = insAreaPolC(pol, C(P(md2, y), r));
            if (z1 < z2) {
                lw = md1;
            } else {
                up = md2;
            }
        }
        return insAreaPolC(pol, C(P(lw, y), r));
    };
    
    D lw = ymi, up = yma;
    for (int ph = 0; ph < 50; ph++) {
        D md1 = (lw+lw+up) / 3;
        D md2 = (lw+up+up) / 3;
        if (calc(md1) < calc(md2)) {
            lw = md1;
        } else {
            up = md2;
        }
    }

    cout << calc(lw) << endl;
    return 0;
}