Problem p00249 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

typedef double D;
typedef complex<D> P;

const D EPS = 1e-8;

D calc(const vector<P> &plg, D v) {
  int n = plg.size();
  int lt = n - 1, rt = 2;
  D lx = 0, rx = real(plg[1]);
  D y = 0;
  while (1) {
    D ny = min(imag(plg[lt]), imag(plg[rt]));
    D nlx = lx + (ny - y) / (imag(plg[lt]) - y) * (real(plg[lt]) - lx);
    D nrx = rx + (ny - y) / (imag(plg[rt]) - y) * (real(plg[rt]) - rx);
    D w1 = rx - lx, w2 = nrx - nlx;
    D h = ny - y;
    D s = (w1 + w2) * h * 0.5;
    if (s < v) {
      v -= s;
      y = ny;
      lx = nlx;
      rx = nrx;
      if (imag(plg[lt]) == ny) {
        --lt;
      }
      if (imag(plg[rt]) == ny) {
        ++rt;
      }
      assert(lt > rt);
    } else {
      D ret = y;
      D a = (w2 - w1) / h * 0.5;
      D b = w1;
      D c = -v;
      if (abs(a) > EPS) {
        D d = b * b - 4 * a * c;
        assert(d >= 0);
        d = sqrt(d);
        D t1 = (-b + d) / a * 0.5;
        D t2 = (-b - d) / a * 0.5;
        if (t1 > t2) {
          swap(t1, t2);
        }
        assert(t2 >= 0);
        if (t1 >= 0) {
          ret += t1;
        } else {
          ret += t2;
        }
      } else {
        D t = -c / b;
        assert(t >= 0);
        ret += t;
      }
      return ret;
    }
  }
}

int main() {
  cout << fixed << setprecision(9);
  ios::sync_with_stdio(false);

  int n;
  D d, v;
  while (cin >> n >> d >> v, n) {
    v /= d;
    vector<P> pt(n), plg(n);
    for (int i = 0; i < n; ++i) {
      D x, y;
      cin >> x >> y;
      pt[i] = P(x, y);
    }

    D ans = -1;
    for (int i = 0; i < n; ++i) {
      P rot = polar(D(1), -arg(pt[(i + 1) % n] - pt[i]));
      for (int j = 0; j < n; ++j) {
        plg[j] = (pt[(i + j) % n] - pt[i]) * rot;
      }
      D t = calc(plg, v);
      ans = max(ans, t);
    }
    cout << ans << '\n';
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

typedef double D;
typedef complex<D> P;

const D EPS = 1e-8;

D calc(const vector<P> &plg, D v) {
  int n = plg.size();
  int lt = n - 1, rt = 2;
  D lx = 0, rx = real(plg[1]);
  D y = 0;
  while (1) {
    D ny = min(imag(plg[lt]), imag(plg[rt]));
    D nlx = lx + (ny - y) / (imag(plg[lt]) - y) * (real(plg[lt]) - lx);
    D nrx = rx + (ny - y) / (imag(plg[rt]) - y) * (real(plg[rt]) - rx);
    D w1 = rx - lx, w2 = nrx - nlx;
    D h = ny - y;
    D s = (w1 + w2) * h * 0.5;
    if (s < v) {
      v -= s;
      y = ny;
      lx = nlx;
      rx = nrx;
      if (imag(plg[lt]) == ny) {
        --lt;
      }
      if (imag(plg[rt]) == ny) {
        ++rt;
      }
      assert(lt >= rt);
    } else {
      D ret = y;
      D a = (w2 - w1) / h * 0.5;
      D b = w1;
      D c = -v;
      if (abs(a) > EPS) {
        D d = b * b - 4 * a * c;
        assert(d >= 0);
        d = sqrt(d);
        D t1 = (-b + d) / a * 0.5;
        D t2 = (-b - d) / a * 0.5;
        if (t1 > t2) {
          swap(t1, t2);
        }
        assert(t2 >= 0);
        if (t1 >= 0) {
          ret += t1;
        } else {
          ret += t2;
        }
      } else {
        D t = -c / b;
        assert(t >= 0);
        ret += t;
      }
      return ret;
    }
  }
}

int main() {
  cout << fixed << setprecision(9);
  ios::sync_with_stdio(false);

  int n;
  D d, v;
  while (cin >> n >> d >> v, n) {
    v /= d;
    vector<P> pt(n), plg(n);
    for (int i = 0; i < n; ++i) {
      D x, y;
      cin >> x >> y;
      pt[i] = P(x, y);
    }

    D ans = -1;
    for (int i = 0; i < n; ++i) {
      P rot = polar(D(1), -arg(pt[(i + 1) % n] - pt[i]));
      for (int j = 0; j < n; ++j) {
        plg[j] = (pt[(i + j) % n] - pt[i]) * rot;
      }
      D t = calc(plg, v);
      ans = max(ans, t);
    }
    cout << ans << '\n';
  }
}