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

#include <algorithm>
#include <assert.h>
#include <complex>
#include <stdio.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef complex<double> P;
#define EPS (1e-12)
#define e (1e-6)

P readP() {
  double x, y;
  scanf("%lf%lf", &x, &y);
  return P(x, y);
}

double radius(const vector<P> &ps) {
  P p = P(0, 0);
  for (double cur = 1.0; cur > EPS; cur *= 0.5) {
    rep(_, 100) {
      double mx = norm(ps[0] - p);
      int ix = 0;
      rep(i, ps.size()) {
        double d = norm(ps[i] - p);
        if (mx < d)
          mx = d, ix = i;
      }
      p += cur * (ps[ix] - p);
    }
  }
  double ans = 0;
  rep(i, ps.size()) ans = max(ans, abs(ps[i] - p));
  return ans;
}

int n, m;
double rs[128], ts[128];
bool used[128];

bool can(int k) {
  vector<double> a, b;
  rep(i, n) if (!used[i]) a.push_back(rs[i]);
  rep(i, m) if (i >= k) b.push_back(ts[i]);
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  sort(b.begin(), b.end());
  reverse(b.begin(), b.end());
  rep(i, b.size()) if (b[i] > a[i] + e) return false;
  return true;
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, n) scanf("%lf", rs + i);
  rep(i, m) {
    int K;
    scanf("%d", &K);
    vector<P> ps;
    rep(_, K) ps.push_back(readP());
    ts[i] = radius(ps);
  }
  if (!can(0))
    puts("NG");
  else {
    int c = 0;
    rep(k, m) {
      rep(i, n) if (!used[i] && ts[k] <= rs[i] + e) {
        used[i] = 1;
        if (can(k + 1)) {
          c++;
          printf("%d\\n", i + 1);
          break;
        }
        used[i] = 0;
      }
    }
    assert(c == m);
  }
  return 0;
}

Error: -6
3ac43e3a-64a8-4b77-9b6c-1120b2ee175a.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p01537/C++/s735449685.cpp:84: int main(): Assertion `used[i] == 1' failed.


Fixed submission:

#include <algorithm>
#include <assert.h>
#include <complex>
#include <stdio.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef complex<double> P;
#define EPS (1e-2)

inline P unit(const P &p) { return 1 / abs(p) * p; }

P readP() {
  double x, y;
  scanf("%lf%lf", &x, &y);
  return P(x, y);
}

double f(const vector<P> &ps, P base, double z, P dir) {
  double l = 0, r = 1024;
  rep(_, 50) {
    const double rad = (l + r) / 2;
    bool ok = true;
    if (rad < z)
      ok = false;
    else {
      const P c = base + sqrt(rad * rad - z * z) * dir;
      const double r2 = rad * rad + EPS;
      rep(i, ps.size()) if (norm(ps[i] - c) > r2) {
        ok = false;
        break;
      }
    }
    if (ok)
      r = rad;
    else
      l = rad;
  }
  return r;
}

double radius(const vector<P> &ps) {
  double ans = 1e100;
  rep(i, ps.size()) rep(j, i) {
    const P m = 0.5 * (ps[i] + ps[j]);
    const double z = abs(ps[i] - m);
    ans = min(ans, f(ps, m, z, unit(ps[j] - ps[i]) * P(0, 1)));
    ans = min(ans, f(ps, m, z, unit(ps[j] - ps[i]) * P(0, -1)));
  }
  return ans;
}

int n, m;
double rs[128], ts[128];
bool used[128];

bool can(int k) {
  vector<double> a, b;
  rep(i, n) if (!used[i]) a.push_back(rs[i]);
  rep(i, m) if (i >= k) b.push_back(ts[i]);
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  sort(b.begin(), b.end());
  reverse(b.begin(), b.end());
  rep(i, b.size()) if (b[i] > a[i] + EPS) return false;
  return true;
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, n) scanf("%lf", rs + i);
  rep(i, m) {
    int K;
    scanf("%d", &K);
    vector<P> ps;
    rep(_, K) ps.push_back(readP());
    ts[i] = radius(ps);
  }
  if (!can(0))
    puts("NG");
  else {
    rep(k, m) {
      rep(i, n) if (!used[i] && ts[i] < rs[i] + EPS) {
        used[i] = 1;
        if (can(k + 1)) {
          printf("%d\\n", i + 1);
          break;
        }
        used[i] = 0;
      }
    }
    rep(i, n) assert(used[i] == 1);
  }
  return 0;
}