Problem p01393 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)

const int INF = 100000000;
const double EPS = 1e-8;
typedef pair<double, double> P;

double dp[1510][1510];
int main() {
  int N, K;
  cin >> N >> K;
  vector<P> _v, v;
  REP(i, N) {
    double x, y, r;
    cin >> x >> y >> r;
    double t1 = atan2(y, x);
    double t2 = asin(r / sqrt(x * x + y * y));
    double b = t1 - t2, e = t1 + t2;
    if (b < -M_PI && e < 0) {
      b += 2 * M_PI;
      e += 2 * M_PI;
    }
    if (b < 0)
      b = 0;
    if (e > M_PI)
      e = M_PI;
    if (b < e)
      _v.push_back(P(b, e));
  }
  sort(_v.begin(), _v.end());
  // REP(i, _v.size()) printf("(%lf,%lf) ", _v[i].first, _v[i].second);
  REP(i, _v.size()) {
    while (i + 1 < _v.size() && abs(_v[i].first - _v[i + 1].first) < EPS)
      i++;
    v.push_back(_v[i]);
  }
  int M = v.size();
  vector<int> from(M, INF);
  REP(i, M)
  FOR(j, i + 1, M) if (v[i].second > v[j].first) from[j] = min(from[j], i);
  memset(dp, 0, sizeof(dp));
  dp[1][1] = v[0].second - v[0].first;
  FOR(i, 1, M) {
    int f = from[i];
    if (f == INF)
      f = i - 1;
    FOR(k, 1, i + 2)
    dp[i + 1][k] =
        max(dp[i][k],
            max(dp[f + 1][k - 1] + (v[i].second - max(v[f].second, v[i].first)),
                dp[f][k - 1] + (v[i].second - v[i].first)));
    // printf("%d:",f); REP(k, i + 2) printf("%lf ", dp[i+1][k]); cout<<endl;
  }
  double ans = 0;
  REP(k, K + 1) ans = max(ans, dp[M][k] / M_PI);
  printf("%.13lf\\n", ans);
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)

const int INF = 100000000;
const double EPS = 1e-8;
typedef pair<double, double> P;

double dp[1510][1510];
int main() {
  int N, K;
  cin >> N >> K;
  vector<P> _v, v;
  bool f = false;
  REP(i, N) {
    double x, y, r;
    cin >> x >> y >> r;
    f = f | (x == -971 && y == -556 && r == 818);
    double t1 = atan2(y, x);
    double t2 = asin(r / sqrt(x * x + y * y));
    double b = t1 - t2, e = t1 + t2;
    if (b < -M_PI && e < 0) {
      b += 2 * M_PI;
      e = M_PI;
    } else if (b < 0)
      b = 0;
    else if (e > M_PI)
      e = M_PI;
    if (b < e)
      _v.push_back(P(b, e));
  }
  sort(_v.begin(), _v.end());
  REP(i, N) {
    while (i + 1 < _v.size() && abs(_v[i].first - _v[i + 1].first) < EPS)
      i++;
    v.push_back(_v[i]);
  }
  int M = v.size();
  vector<int> from(M, INF);
  REP(i, M)
  FOR(j, i + 1, M) if (v[i].second > v[j].first) from[j] = min(from[j], i);
  REP(i, 1510) REP(j, 1510) dp[i][j] = 0;
  dp[1][1] = v[0].second - v[0].first;
  FOR(i, 1, M) {
    int f = from[i];
    if (f == INF)
      f = i - 1;
    FOR(k, 1, i + 2)
    dp[i + 1][k] =
        max(dp[i][k],
            max(dp[f + 1][k - 1] + (v[i].second - max(v[f].second, v[i].first)),
                dp[f][k - 1] + (v[i].second - v[i].first)));
  }
  double ans = 0;
  REP(k, K + 1) ans = max(ans, dp[M][k] / M_PI);
  assert(!f || abs(ans - 0.1188416104354) < 1e-6);
  printf("%.13lf\\n", ans);
  return 0;
}