alexjercan/bugnet huggingface dataset:
Problem p02303 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<double, double> P;

int n;
pair<double, double> p[100000];

int main() {
  int i, j;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> p[i].first >> p[i].second;

  // random rotate
  double rad = 0.364364334 * 3.14159265358979;
  for (i = 0; i < n; i++) {
    double x = p[i].first * cos(rad) - p[i].second * sin(rad);
    double y = p[i].first * sin(rad) + p[i].second * cos(rad);
    p[i].first = x;
    p[i].second = y;
  }

  sort(p, p + n);

  double minDist2 = 1145141919;

  for (i = 0; i < n; i++) {
    for (j = i - 1; j >= 0; j--) {
      double dx = p[i].first - p[j].first;
      double dy = p[i].second - p[j].second;
      double dist = dx * dx + dy * dy;

      if (dx * dx >= minDist2)
        break;

      minDist2 = min(minDist2, dist);
    }
  }

  printf("%.14f\\n", sqrt(minDist2));
  return 0;
}

Error: TLE

Fixed submission:

// TLE
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<double, double> P;

int n;
pair<double, double> p[100000];

int main() {
  int i, j;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> p[i].first >> p[i].second;

  sort(p, p + n);

  double minDist2 = 1145141919;

  for (i = 0; i < n; i++) {
    for (j = i - 1; j >= 0; j--) {
      double dx = p[i].first - p[j].first;
      double dy = p[i].second - p[j].second;
      double dist = dx * dx + dy * dy;

      if (dx * dx >= minDist2)
        break;

      minDist2 = min(minDist2, dist);
    }
  }

  printf("%.14f\\n", sqrt(minDist2));
  return 0;
}
Problem p02303 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define MAX_N 100000
const double INF = 1e50;

typedef pair<double, double> P;

int N;
P A[MAX_N];

bool compare_y(const P &a, const P &b) { return a.second < b.second; }

double closest_pair(P *a, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  double x = a[m].first;
  double d = min(closest_pair(a, m), closest_pair(a + m, n - m));
  inplace_merge(a, a + m, a + n, compare_y);

  vector<P> b;
  for (int i = 0; i < n; i++) {
    if (fabs(a[i].first - x) >= d)
      continue;
    for (int j = 0; j < (int)b.size(); j++) {
      double dx = a[i].first - b[b.size() - j - 1].first;
      double dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break;
      d = min(d, sqrt(dx * dx + dy * dy));
    }
    b.push_back(a[i]);
  }
  return d;
}

void solve() {
  sort(A, A + N);
  printf("%.8f\\n", closest_pair(A, N));
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%lf%lf", &A[i].first, &A[i].second);
  }
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX_N 100000
const double INF = 1e50;

typedef pair<double, double> P;

int N;
P A[MAX_N];

bool compare_y(const P &a, const P &b) { return a.second < b.second; }

double closest_pair(P *a, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  double x = a[m].first;
  double d = min(closest_pair(a, m), closest_pair(a + m, n - m));
  inplace_merge(a, a + m, a + n, compare_y);

  vector<P> b;
  for (int i = 0; i < n; i++) {
    if (fabs(a[i].first - x >= d))
      continue;
    for (int j = 0; j < (int)b.size(); j++) {
      double dx = a[i].first - b[b.size() - j - 1].first;
      double dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break;
      d = min(d, sqrt(dx * dx + dy * dy));
    }
    b.push_back(a[i]);
  }
  return d;
}

void solve() {
  sort(A, A + N);
  printf("%.8f\\n", closest_pair(A, N));
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%lf%lf", &A[i].first, &A[i].second);
  }
  solve();
  return 0;
}
Problem p02303 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

int main() {
  int n;
  double x[100000], y[100000];

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%lf", &x[i]);
    scanf("%lf", &y[i]);
  }

  pair<double, double> p1[100000], p2[100000];

  for (int i = 0; i < n; i++) {
    p1[i] = make_pair(x[i], y[i]);
  }

  for (int i = 0; i < n; i++) {
    p2[i] = make_pair(y[i], x[i]);
  }

  sort(p1, p1 + n);
  sort(p2, p2 + n);

  double sqr_ans = 100000000.0;

  for (int i = 0; i < n - 1; i++) {
    int r = min(5000, n - i - 1);

    for (int j = 1; j <= r; j++) {
      double distance_ =
          (p1[i + j].first - p1[i].first) * (p1[i + j].first - p1[i].first) +
          (p1[i + j].second - p1[i].second) * (p1[i + j].second - p1[i].second);

      sqr_ans = min(distance_, sqr_ans);
    }
  }

  for (int i = 0; i < n - 1; i++) {
    int r = min(5000, n - i - 1);

    for (int j = 1; j <= r; j++) {
      double distance_ =
          (p2[i + 1].first - p2[i].first) * (p2[i + 1].first - p2[i].first) +
          (p2[i + 1].second - p2[i].second) * (p2[i + 1].second - p2[i].second);

      sqr_ans = min(distance_, sqr_ans);
    }
  }

  printf("%.15f\\n", sqrt(sqr_ans));

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

int main() {
  int n;
  float x[100000], y[100000];

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%f", &x[i]);
    scanf("%f", &y[i]);
  }

  float sqr_ans = 100000000.0;

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sqr_ans =
          min((x[j] - x[i]) * (x[j] - x[i]) + (y[j] - y[i]) * (y[j] - y[i]),
              sqr_ans);
    }
  }

  printf("%.15f\\n", sqrt(sqr_ans));

  return 0;
}
Problem p02303 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

typedef complex<double> Point;
typedef vector<Point> VP;
#define X real()
#define Y imag()
const double INF = 1e9;

bool compX(const Point a, const Point b) {
  return (a.X != b.X ? a.X < b.X : a.Y < b.Y);
}

bool compY(const Point a, const Point b) { return a.Y < b.Y; }

double closestPair(Point *a, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  double x = a[m].X;
  double d = min(closestPair(a, m), closestPair(a + m, n - m));
  inplace_merge(a, a + m, a + n, compY);

  VP b;
  rep(i, n) {
    if (abs(a[i].X - x) >= d)
      continue;
    for (int j = b.size() - 1; j >= 0; j--) {
      Point ba = a[i] - b[j];
      if (ba.Y >= d)
        break;
      d = min(d, abs(ba));
    }
    b.push_back(a[i]);
  }
  return d;
}
int main(void) {
  int n;
  cin >> n;
  Point ps[n];
  rep(i, n) {
    double x, y;
    cin >> x >> y;
    ps[i] = Point(x, y);
  }
  sort(ps, ps + n, compX);
  printf("%.9f\\n", closestPair(ps, n));
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)

typedef complex<double> Point;
typedef vector<Point> VP;
#define X real()
#define Y imag()

namespace std {
bool operator<(const Point a, const Point b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int main(void) {
  int n;
  cin >> n;
  VP ps(n);
  rep(i, n) {
    double x, y;
    cin >> x >> y;
    ps[i] = Point(x, y);
  }

  sort(ps.begin(), ps.end());
  double res = 1e9;
  rep(i, n) for (int j = i + 1; j < n; j++) {
    Point dx = Point(ps[j].X, ps[i].Y);
    if (abs(dx - ps[i]) > res)
      break;
    res = min(res, abs(ps[i] - ps[j]));
  }
  printf("%.9f\\n", res);
  return 0;
}
Problem p02303 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define pt pair<double, double>
#define x first
#define y second
const double INF = 1e7;
pt p[100010];
double pw2(double d) { return d * d; }
double dist(pt a, pt b) { return sqrt(pw2(a.x - b.x) + pw2(a.y - b.y)); }
bool cmp(pt a, pt b) { return a.y < b.y; }
double closest(int l, int r) {
  if (l == r)
    return INF;
  int mid = (l + r) / 2, pp = l, qq = mid + 1;
  double midline = p[mid].x;
  double ret = min(closest(l, mid), closest(mid + 1, r));
  vector<pt> slab;
  inplace_merge(p + l, p + mid + 1, p + r + 1, cmp);
  for (int i = l; i < r; i++)
    if (p[i].y > p[i + 1].y)
      cout << "ERROR\\n";
  // conquer
  for (int i = l; i <= r; i++)
    if (abs(p[i].x - midline) < ret)
      slab.push_back(p[i]);
  for (int i = 0; i < slab.size(); i++)
    for (int j = i + 1; j < slab.size() && abs(slab[i].y - slab[j].y) < ret;
         j++)
      ret = min(ret, dist(slab[i], slab[j]));
  return ret;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> p[i].x >> p[i].y;
  sort(p + 1, p + n + 1);
  cout << fixed << closest(1, n) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pt pair<double, double>
#define x first
#define y second
const double INF = 1e5, EPS = 1e-9;
pt p[100010];
double pw2(double d) { return d * d; }
double dist(pt a, pt b) { return sqrt(pw2(a.x - b.x) + pw2(a.y - b.y)); }
double closest(int l, int r) {
  if (l == r)
    return INF;
  int mid = (l + r) / 2, pp = l, qq = mid + 1;
  double midline = p[mid].x;
  double ret = min(closest(l, mid), closest(mid + 1, r));
  vector<pt> merge, slab;
  while (pp <= mid || qq <= r)
    if (pp > mid)
      merge.push_back(p[qq++]);
    else if (qq > r)
      merge.push_back(p[pp++]);
    else
      merge.push_back(p[pp].y < p[qq].y ? p[pp++] : p[qq++]);
  pp = l;
  for (int i = 0; i < merge.size(); i++)
    p[pp++] = merge[i];
  for (int i = l; i <= r; i++)
    // if(abs(p[i].x-midline)+EPS<ret)
    slab.push_back(p[i]);
  for (int i = 0; i < slab.size(); i++)
    for (int j = i + 1; j < slab.size() && (slab[j].y - slab[i].y) < ret; j++)
      ret = min(ret, dist(slab[i], slab[j]));
  return ret;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> p[i].x >> p[i].y;
  cout << fixed << closest(1, n) << endl;
  return 0;
}
Problem p02303 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;
typedef pair<double, double> pp;
vector<pp> a;

#define sqr(x) ((x) * (x))
double Plus(double x) { return x < 0 ? -x : x; }
double dist(pp x, pp y) {
  return sqr(x.first - y.first) + sqr(x.second - y.second);
}

double go(int l, int r) {
  if (r - l == 1)
    return dist(a[l], a[r]);
  if (r - l == 2)
    return min(dist(a[l], a[l + 1]),
               min(dist(a[l], a[l + 2]), dist(a[l + 1], a[l + 2])));

  int m = (l + r) / 2;
  double d = min(go(l, m), go(m + 1, r));

  vector<pp> v;
  for (auto i = l; i <= r; i++)
    if (sqr(a[i].first - a[m].first) < d)
      v.emplace_back(a[i].second, a[i].first);
  sort(v.begin(), v.end());
  for (auto i = v.begin(); i != v.end(); i++)
    for (auto j = i + 1; j != v.end(); j++) {
      if (sqr(j->first - i->first) > d)
        break;
      d = min(d, dist(*i, *j));
    }
  return d;
}

int main() {
  int i, n;
  double x, y;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%lf%lf", &x, &y), a.emplace_back(x, y);
  sort(a.begin(), a.end());
  printf("%lf ", sqrt(go(0, n - 1)));
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;
typedef pair<double, double> pp;
vector<pp> a;

#define sqr(x) ((x) * (x))
double Plus(double x) { return x < 0 ? -x : x; }
double dist(pp x, pp y) {
  return sqr(x.first - y.first) + sqr(x.second - y.second);
}

double go(int l, int r) {
  if (r - l == 1)
    return dist(a[l], a[r]);
  if (r - l == 2)
    return min(dist(a[l], a[l + 1]),
               min(dist(a[l], a[l + 2]), dist(a[l + 1], a[l + 2])));

  int m = (l + r) / 2;
  double d = min(go(l, m), go(m + 1, r));

  vector<pp> v;
  for (auto i = l; i <= r; i++)
    if (sqr(a[i].first - a[m].first) < d)
      v.emplace_back(a[i].second, a[i].first);
  sort(v.begin(), v.end());
  for (auto i = v.begin(); i != v.end(); i++)
    for (auto j = i + 1; j != v.end(); j++) {
      d = min(d, dist(*i, *j));
    }
  return d;
}

int main() {
  int i, n;
  double x, y;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%lf%lf", &x, &y), a.emplace_back(x, y);
  sort(a.begin(), a.end());
  printf("%lf ", sqrt(go(0, n - 1)));
}
Problem p02303 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <utility>
#include <vector>

typedef pair<double, double> point;
typedef point *data;

inline double dist(const point &a, const point &b) {
  double dx = a.first - b.first, dy = a.second - b.second;
  return sqrt(dx * dx + dy * dy);
}

bool sort_y(const point &P1, const point &P2) { return P1.second < P2.second; }

double closest(data P, int n) {
  if (n <= 1)
    return 1000;
  if (n == 2)
    return dist(P[0], P[1]);
  int m = n / 2;
  sort(P, P + n, sort_y); // y_sort, down to up.
  double d = min(closest(P, m), closest(P + m, n - m));

  sort(P, P + m);
  sort(P + m, P + n);
  // x_sort, left to right.
  int a, b, u, v, i, j;
  a = u = i = 0, b = v = j = m;
  while (i < m && j < n) {
    if (P[u].first < P[v].first) {
      for (i = a; i < m; i++) {
        if (P[i].first >= P[v].first) {
          u = i;
          break;
        }
        if (P[v].first - P[i].first >= d) {
          a++;
        } else {
          d = min(d, dist(P[v], P[i]));
        }
      }
    } else {
      for (j = b; j < n; j++) {
        if (P[j].first > P[u].first) {
          v = j;
          break;
        }
        if (P[u].first - P[j].first >= d) {
          b++;
        } else {
          d = min(d, dist(P[u], P[j]));
        }
      }
    }
  };
  return d;
}

int main() {
  int i, n;
  double x, y;
  scanf("%d", &n);
  data P;
  P = new point[n];

  for (i = 0; i < n; i++) {
    scanf("%lf %lf", &x, &y);
    P[i] = make_pair(x, y);
  }
  printf("%.12f\\n", closest(P, n));

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <utility>
#include <vector>

typedef pair<double, double> point;
typedef vector<point> data;

inline double dist(const point &a, const point &b) {
  double dx = a.first - b.first, dy = a.second - b.second;
  return sqrt(dx * dx + dy * dy);
}

int main() {
  int i, n;
  double x, y;
  scanf("%d", &n);
  data P(n);

  for (i = 0; i < n; i++) {
    scanf("%lf %lf", &x, &y);
    P[i] = make_pair(x, y);
  }
  sort(P.begin(), P.end());

  double d = dist(P[0], P[1]);
  for (i = 2; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (P[i].first - P[j].first > d)
        continue;
      d = min(d, dist(P[i], P[j]));
    }
  }
  printf("%.12f\\n", d);
  return 0;
}
Problem p02308 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(a) a.begin(), a.end()
typedef complex<double> Point;
typedef vector<Point> VP;
#define X real()
#define Y imag()
const double EPS = 1e-12;

#define GE(n, m) (EPS > (m) - (n))

double dot(Point a, Point b) { return a.X * b.X + a.Y * b.Y; }

Point proj(Point a1, Point a2, Point p) {
  return a1 + dot(a2 - a1, p - a1) / norm(a2 - a1) * (a2 - a1);
}

VP crosspointLC(Point a1, Point a2, Point c, double r) {
  VP ps;
  Point ft = proj(a1, a2, c);
  if (!GE(r * r, norm(ft - c)))
    assert(false);
  Point dir = sqrt(max(r * r - norm(ft - c), 0.0)) / abs(a2 - a1) * (a2 - a1);
  ps.push_back(ft + dir);
  ps.push_back(ft - dir);
  return ps;
}

namespace std {
bool operator<(const Point a, const Point b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int main(void) {
  double cx, cy, r;
  int q;
  cin >> cx >> cy >> r >> q;
  rep(i, q) {
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    VP ps = crosspointLC(Point(x1, y1), Point(x2, y2), Point(cx, cy), r);
    sort(all(ps));
    printf("%.9f %.9f ", ps[0].X, ps[0].Y);
    printf("%.9f %.9f\\n", ps[1].X, ps[1].Y);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(a) a.begin(), a.end()
typedef complex<double> Point;
typedef vector<Point> VP;
#define X real()
#define Y imag()
const double EPS = 1e-9;

#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

double dot(Point a, Point b) { return a.X * b.X + a.Y * b.Y; }

Point proj(Point a1, Point a2, Point p) {
  return a1 + dot(a2 - a1, p - a1) / norm(a2 - a1) * (a2 - a1);
}

VP crosspointLC(Point a1, Point a2, Point c, double r) {
  VP ps;
  Point ft = proj(a1, a2, c);
  if (!GE(r * r, norm(ft - c)))
    assert(false);
  assert(r * r >= norm(ft - c));
  Point dir = sqrt(max(r * r - norm(ft - c), 0.0)) / abs(a2 - a1) * (a2 - a1);
  ps.push_back(ft + dir);
  ps.push_back(ft - dir);
  return ps;
}

namespace std {
bool operator<(const Point a, const Point b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int main(void) {
  double cx, cy, r;
  int q;
  cin >> cx >> cy >> r >> q;
  rep(i, q) {
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    VP ps = crosspointLC(Point(x1, y1), Point(x2, y2), Point(cx, cy), r);
    sort(all(ps));
    printf("%.9f %.9f ", ps[0].X, ps[0].Y);
    printf("%.9f %.9f\\n", ps[1].X, ps[1].Y);
  }
  return 0;
}
Problem p02308 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(a) a.begin(), a.end()
#define EPS 1e-12
typedef complex<double> Point;
typedef vector<Point> VP;
#define X real()
#define Y imag()
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

double dot(Point a, Point b) { return a.X * b.X + a.Y * b.Y; }

Point proj(Point a1, Point a2, Point p) {
  return a1 + dot(a2 - a1, p - a1) / norm(a2 - a1) * (a2 - a1);
}

VP crosspointLC(Point a1, Point a2, Point c, double r) {
  VP ps;
  Point ft = proj(a1, a2, c);
  // if (!GE(r*r, norm(ft-c))) return ps;

  Point dir = sqrt(max(r * r - norm(ft - c), 0.0)) / abs(a2 - a1) * (a2 - a1);
  ps.push_back(ft + dir);
  ps.push_back(ft - dir);
  return ps;
}

namespace std {
bool operator<(const Point a, const Point b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int main(void) {
  double cx, cy, r;
  int q;
  cin >> cx >> cy >> r >> q;
  rep(i, q) {
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    VP ps = crosspointLC(Point(x1, y1), Point(x2, y2), Point(cx, cy), r);
    sort(all(ps));
    printf("%.9f %.9f ", ps[0].X, ps[0].Y);
    printf("%.9f %.9f\\n", ps[1].X, ps[1].Y);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(a) a.begin(), a.end()
#define EPS 1e-10
typedef complex<double> Point;
typedef vector<Point> VP;
#define X real()
#define Y imag()
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

double dot(Point a, Point b) { return a.X * b.X + a.Y * b.Y; }

Point proj(Point a1, Point a2, Point p) {
  return a1 + dot(a2 - a1, p - a1) / norm(a2 - a1) * (a2 - a1);
}

VP crosspointLC(Point a1, Point a2, Point c, double r) {
  VP ps;
  Point ft = proj(a1, a2, c);
  if (!GE(r * r, norm(ft - c)))
    return ps;

  Point dir = sqrt(max(r * r - norm(ft - c), 0.0)) / abs(a2 - a1) * (a2 - a1);
  ps.push_back(ft + dir);
  ps.push_back(ft - dir);
  return ps;
}

namespace std {
bool operator<(const Point a, const Point b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int main(void) {
  double cx, cy, r;
  int q;
  cin >> cx >> cy >> r >> q;
  rep(i, q) {
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    VP ps = crosspointLC(Point(x1, y1), Point(x2, y2), Point(cx, cy), r);
    sort(all(ps));
    printf("%.9f %.9f ", ps[0].X, ps[0].Y);
    printf("%.9f %.9f\\n", ps[1].X, ps[1].Y);
  }
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int ws[23], sum, num, dp[50003];
  cin >> sum >> num;
  for (int i = 0; i < num; i++)
    cin >> ws[i];
  for (int i = 0; i <= sum; i++)
    dp[i] = i;
  for (int i = 1; i < num; i++) {
    for (int j = ws[i]; j <= sum; j++)
      dp[j] = min(dp[j], dp[j - ws[i]] + 1);
  }
  cout << dp[sum] << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int ws[23], sum, num, dp[10003];
  cin >> sum >> num;
  for (int i = 0; i < num; i++)
    cin >> ws[i];
  for (int i = 0; i <= sum; i++)
    dp[i] = i;
  for (int i = 1; i < num; i++) {
    for (int j = ws[i]; j <= sum; j++)
      dp[j] = min(dp[j], dp[j - ws[i]] + 1);
  }
  cout << dp[sum] << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  int dp[50001];
  for (int i = 0; i < 50001; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int d;
    cin >> d;
    for (int i = 0; i + d < 50001; i++) {
      dp[i + d] = min(dp[i + d], dp[i] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int dp[50001];
  for (int i = 0; i < 50001; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int d;
    cin >> d;
    for (int i = 0; i < 50001; i++) {
      dp[i + d] = min(dp[i + d], dp[i] + 1);
    }
  }
  cout << dp[n] << endl;

  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
// #include<cmath>
#define MAX 50000
using namespace std;
int c[21][MAX + 10];

int main() {
  int n, m, sum = 0;
  int coin[MAX];
  cin >> n >> m;
  int i, w;
  for (i = 0; i < m; i++) {
    cin >> coin[i];
  }
  sort(coin, coin + m);
  //    vector<vector<int> > c(m+1,vector<int>(n+1));
  for (int i = 1; i <= n; i++)
    c[1][i] = 1000000;
  c[1][0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = coin[i]; j <= n; j++)
      c[1][j] = min(c[1][j], c[1][j - coin[i]] + 1);
  }
  cout << c[1][n] << endl;
  //    for(i=0;i<=m;i++)
  //    {
  //        c[m][1]=1;
  //        c[m][0]=0;
  //    }
  //    for(w=1;w<=n;w++)
  //    {
  //        c[1][w]=w/coin[0];
  //       //c[0][w]=0;
  //    }
  //
  //    for(i = 2; i <= m; i++)
  //    {
  //        for(w = 2; w <= n; w++)
  //        {
  //            if(w < coin[i-1])
  //            {
  //                c[i][w] = c[i - 1][w];
  //            }
  //            else
  //            {
  //                int x = c[i-1][w];
  //                int y = c[i][w - coin[i-1]] + 1;
  //                c[i][w] = x > y ? y : x;
  //            }
  //        }
  //    }
  //	cout<<c[m][n]<<endl;
  return 0;
}


Error: -6
free(): invalid pointer


Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
// #include<cmath>
#define MAX 50000
using namespace std;

int minn(int x, int y) { return x < y ? x : y; }

int main() {
  int n, m, s, sum = 0;
  int coin[MAX] = {0};
  cin >> n >> m;
  for (int i = 0; i < m;) {
    cin >> s;
    if (s <= n)
      coin[i++] = s;
    else
      m--;
  }
  vector<vector<int>> c(n + 1, vector<int>(m + 1));
  int i, w;
  c[0][1] = 0;
  c[1][0] = 0;
  c[0][0] = 0;
  c[1][1] = 1; // sum[0]
  for (i = 1; i <= m; i++) {
    for (w = 1; w <= n; w++) {
      if (w < coin[i - 1]) {
        c[i][w] = c[i - 1][w];
      } else {
        int x = c[i - 1][w];
        int y = c[i][w - coin[i - 1]] + 1;
        c[i][w] = x > y ? y : x;
      }
    }
  }
  cout << c[n][m] << "\\r\\n";
  return 0;
}