Problem p02926 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define mp make_pair

#ifndef ONLINE_JUDGE
#define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#define LOG(x) ((void)0)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const double PI = 3.1415926535897932384626433832795;
const ll INF = 1LL << 62;
const ll MINF = -1LL << 62;

template <typename T> T getint() {
  T val = 0;
  char c;

  bool neg = false;
  while ((c = gc()) && !(c >= '0' && c <= '9')) {
    neg |= c == '-';
  }

  do {
    val = (val * 10) + c - '0';
  } while ((c = gc()) && (c >= '0' && c <= '9'));

  return val * (neg ? -1 : 1);
}

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// uniform_int_distribution<int>(0, n-1)(rng)

typedef pair<ll, ll> pt;

int ccw(pt a, pt b, pt c) {
  b.xx -= a.xx;
  c.xx -= a.xx;
  b.yy -= a.yy;
  c.yy -= a.yy;

  if (b.yy * c.xx < c.yy * b.xx) {
    return -1;
  } else if (b.yy * c.xx == c.yy * b.xx) {
    return 0;
  }

  return 1;
}

ll sqdist(pt a, pt b) {
  return (a.xx - b.xx) * (a.xx - b.xx) + (a.yy - b.yy) * (a.yy - b.yy);
}

int main() {
  IO;
  cout << fixed << setprecision(20);
  int n;
  cin >> n;
  vector<pt> lst_(n);
  for (int i = 0; i < n; ++i) {
    cin >> lst_[i].xx >> lst_[i].yy;
  }
  vector<pt> lst;
  for (int i = 0; i < n; ++i) {
    if (lst_[i].xx == 0 && lst_[i].yy == 0)
      continue;
    lst.push_back(lst_[i]);
  }
  n = lst.size();

  pt orig = {0, 0};
  while (1) {
    bool change = false;
    for (int i = 1; i < n; ++i) {
      if ((ccw(orig, lst[0], lst[i]) == 0 &&
           sqdist(orig, lst[i]) < sqdist(orig, lst[0]))) {
        change = true;
        swap(lst[0], lst[i]);
      }
    }
    if (!change)
      break;
  }

  if (n == 0) {
    cout << 0.0 << "\n";
  } else if (n == 1) {
    cout << sqrtl(sqdist(orig, lst[0])) << "\n";
    return 0;
  }

  vector<pt> bal, jobb;
  for (int i = 1; i < n; ++i) {
    if (ccw(orig, lst[0], lst[i]) > 0 ||
        (ccw(orig, lst[0], lst[i]) == 0 &&
         (sqdist(lst[0], lst[i]) <= sqdist(orig, lst[i])))) {
      jobb.push_back(lst[i]);
    } else {
      bal.push_back(lst[i]);
    }
  }

  sort(bal.begin(), bal.end(), [&](pt &egyik, pt &masik) -> bool {
    if (ccw(orig, egyik, masik) == 0)
      return sqdist(orig, egyik) < sqdist(orig, masik);
    return ccw(orig, egyik, masik) > 0;
  });
  sort(jobb.begin(), jobb.end(), [&](pt &egyik, pt &masik) -> bool {
    if (ccw(orig, egyik, masik) == 0)
      return sqdist(orig, egyik) < sqdist(orig, masik);
    return ccw(orig, egyik, masik) > 0;
  });

  vector<pt> osz;
  for (auto i : bal)
    osz.push_back(i);
  osz.push_back(lst[0]);
  for (auto i : jobb)
    osz.push_back(i);

  /*cerr<<lst[0].xx<<" "<<lst[0].yy<<"\n\n";
  for(auto i:osz) cerr<<i.xx<<" "<<i.yy<<"\n";
  */
  vector<pt> ps;
  for (auto i : osz)
    ps.push_back(i);
  for (auto i : osz)
    ps.push_back(i);

  pt best = {0, 0};
  for (int i = 0; i < (int)osz.size(); ++i) {
    pt szum = {0, 0};
    for (int j = i; j < i + (int)osz.size(); ++j) {
      szum.xx += ps[j].xx;
      szum.yy += ps[j].yy;
      if (sqdist(best, orig) < sqdist(szum, orig))
        best = szum;
    }
  }

  cout << sqrtl(sqdist(best, orig)) << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define mp make_pair

#ifndef ONLINE_JUDGE
#define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#define LOG(x) ((void)0)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const double PI = 3.1415926535897932384626433832795;
const ll INF = 1LL << 62;
const ll MINF = -1LL << 62;

template <typename T> T getint() {
  T val = 0;
  char c;

  bool neg = false;
  while ((c = gc()) && !(c >= '0' && c <= '9')) {
    neg |= c == '-';
  }

  do {
    val = (val * 10) + c - '0';
  } while ((c = gc()) && (c >= '0' && c <= '9'));

  return val * (neg ? -1 : 1);
}

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// uniform_int_distribution<int>(0, n-1)(rng)

typedef pair<ll, ll> pt;

int ccw(pt a, pt b, pt c) {
  b.xx -= a.xx;
  c.xx -= a.xx;
  b.yy -= a.yy;
  c.yy -= a.yy;

  if (b.yy * c.xx < c.yy * b.xx) {
    return -1;
  } else if (b.yy * c.xx == c.yy * b.xx) {
    return 0;
  }

  return 1;
}

ll sqdist(pt a, pt b) {
  return (a.xx - b.xx) * (a.xx - b.xx) + (a.yy - b.yy) * (a.yy - b.yy);
}

int main() {
  IO;
  cout << fixed << setprecision(20);
  int n;
  cin >> n;
  vector<pt> lst_(n);
  for (int i = 0; i < n; ++i) {
    cin >> lst_[i].xx >> lst_[i].yy;
  }
  vector<pt> lst;
  for (int i = 0; i < n; ++i) {
    if (lst_[i].xx == 0 && lst_[i].yy == 0)
      continue;
    lst.push_back(lst_[i]);
  }
  n = lst.size();

  pt orig = {0, 0};
  while (1) {
    bool change = false;
    for (int i = 1; i < n; ++i) {
      if ((ccw(orig, lst[0], lst[i]) == 0 &&
           sqdist(orig, lst[i]) < sqdist(orig, lst[0]))) {
        change = true;
        swap(lst[0], lst[i]);
      }
    }
    if (!change)
      break;
  }

  if (n == 0) {
    cout << 0.0 << "\n";
    return 0;
  } else if (n == 1) {
    cout << sqrtl(sqdist(orig, lst[0])) << "\n";
    return 0;
  }

  vector<pt> bal, jobb;
  for (int i = 1; i < n; ++i) {
    if (ccw(orig, lst[0], lst[i]) > 0 ||
        (ccw(orig, lst[0], lst[i]) == 0 &&
         (sqdist(lst[0], lst[i]) <= sqdist(orig, lst[i])))) {
      jobb.push_back(lst[i]);
    } else {
      bal.push_back(lst[i]);
    }
  }

  sort(bal.begin(), bal.end(), [&](pt &egyik, pt &masik) -> bool {
    if (ccw(orig, egyik, masik) == 0)
      return sqdist(orig, egyik) < sqdist(orig, masik);
    return ccw(orig, egyik, masik) > 0;
  });
  sort(jobb.begin(), jobb.end(), [&](pt &egyik, pt &masik) -> bool {
    if (ccw(orig, egyik, masik) == 0)
      return sqdist(orig, egyik) < sqdist(orig, masik);
    return ccw(orig, egyik, masik) > 0;
  });

  vector<pt> osz;
  for (auto i : bal)
    osz.push_back(i);
  osz.push_back(lst[0]);
  for (auto i : jobb)
    osz.push_back(i);

  /*cerr<<lst[0].xx<<" "<<lst[0].yy<<"\n\n";
  for(auto i:osz) cerr<<i.xx<<" "<<i.yy<<"\n";
  */
  vector<pt> ps;
  for (auto i : osz)
    ps.push_back(i);
  for (auto i : osz)
    ps.push_back(i);

  pt best = {0, 0};
  for (int i = 0; i < (int)osz.size(); ++i) {
    pt szum = {0, 0};
    for (int j = i; j < i + (int)osz.size(); ++j) {
      szum.xx += ps[j].xx;
      szum.yy += ps[j].yy;
      if (sqdist(best, orig) < sqdist(szum, orig))
        best = szum;
    }
  }

  cout << sqrtl(sqdist(best, orig)) << "\n";
  return 0;
}