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

#include <algorithm>
#include <complex>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

//???????????¨
bool lt(const complex<double> &a, const complex<double> &b) {
  return a.real() < b.real() || (a.real() == b.real() && a.imag() < b.imag());
}

double cp(complex<double> a, complex<double> b) { return (conj(a) * b).imag(); }

int main() {
  int n;
  cin >> n;

  vector<complex<double>> v;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back(complex<double>(x + 0.0, y + 0.0));
  }

  sort(v.begin(), v.end(), lt);

  vector<complex<double>> u, b;
  u.push_back(v[0]);
  int i;
  for (i = 0; i < n; i++) {
    if (v[i].real() != v[0].real())
      break;
  }
  b.push_back(v[0]);
  b.push_back(v[i]);

  i = i + 1;
  while (b.back() != v.back()) {
    b.push_back(v[i]);
    i++;

    int l = b.size();
    if (l < 3)
      continue;
    if (cp(b[l - 1] - b[l - 2], b[l - 3] - b[l - 2]) >= 0)
      continue;
    else {
      b.pop_back();
      b.pop_back();
      i--;
    }
  }

  i = 1;
  while (u.back() != v.back()) {
    u.push_back(v[i]);
    i++;

    int l = u.size();
    if (l < 3)
      continue;
    if (cp(u[l - 1] - u[l - 2], u[l - 3] - u[l - 2]) <= 0)
      continue;
    else {
      u.pop_back();
      u.pop_back();
      i--;
    }
  }

  double min = b[0].imag(), index = 0;
  for (int i = 1; i < b.size(); i++) {
    if (b[i].imag() < min) {
      min = b[i].imag();
      index = i;
    }
  }

  cout << (b.size() + u.size() - 2) << endl;
  for (int i = index; i < b.size(); i++)
    cout << noshowpoint << b[i].real() << ' ' << b[i].imag() << endl;
  for (int i = u.size() - 2; i >= 1; i--)
    cout << noshowpoint << u[i].real() << ' ' << u[i].imag() << endl;
  for (int i = 0; i < index; i++)
    cout << noshowpoint << b[i].real() << ' ' << b[i].imag() << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
using namespace std;

bool cp(const complex<double> &a, const complex<double> &b) {
  return (a.real() < b.real()) || (a.real() == b.real() && a.imag() < b.imag());
}

int main() {
  int n;
  cin >> n;
  vector<complex<double>> v;

  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back(complex<double>(x, y));
  }

  sort(v.begin(), v.end(), cp);

  vector<complex<double>> u, b;
  int i;
  for (i = 0; v[i].real() == v[0].real(); i++)
    ;
  u.push_back(v[i - 1]);
  b.push_back(v[0]);

  int j = i;
  while (u.back().real() != v.back().real()) {
    u.push_back(v[j]); //??\\????????????
    int l = u.size();

    // l=2 ???
    // (l>3and(u[l-2]-u[l-3]?????????u[l-1]-u[l-2]??????????§??????§??????))??????OK
    //???????????°?¬????????????????????????????????????????????????????????????´???

    if (l == 2)
      j++;
    else if (arg(u[l - 2] - u[l - 3]) > arg(u[l - 1] - u[l - 2]))
      j++;
    else {
      u.pop_back();
      u.pop_back();
    }
  }
  u.pop_back();
  u.push_back(v[n - 1]);

  j = i;
  while (b.back().real() != v.back().real()) {
    b.push_back(v[j]); //??\\????????????
    int l = b.size();

    // l=2 ???
    // (l>3and(b[l-2]-b[l-3]?????????b[l-1]-b[l-2]??????????§?????°???????))??????OK
    //???????????°?¬????????????????????????????????????????????????????????????´???

    if (l == 2)
      j++;
    else if (arg(u[l - 2] - u[l - 3]) < arg(u[l - 1] - u[l - 2]))
      j++;
    else {
      u.pop_back();
      u.pop_back();
    }
  }

  if (u.back() == b.back())
    b.pop_back();

  vector<complex<double>> ru;
  for (int k = 0; k < u.size(); k++)
    ru[k] = u[u.size() - 1 - k];

  if (ru.back() == b.front())
    ru.pop_back();

  b.insert(b.end(), ru.begin(), ru.end());

  for (i = b.size(); i < b.size() * 2; i++)
    if (b[(i - 1) % b.size()].imag() > b[i % b.size()].imag() &&
        b[i % b.size()].imag() <= b[(i + 1) % b.size()].imag())
      break;

  cout << b.size() << endl;
  for (int k = 0; k < b.size(); k++)
    cout << b[(i + k) % b.size()].real() << ' ' << b[(i + k) % b.size()].imag()
         << endl;

  return 0;
}