Problem p02926 (C++)

Buggy submission (status: Time Limit Exceeded):
#if __has_include("../library/Basic/Debug.hpp")

#include "../library/Basic/Debug.hpp"

#else

/* ----- Header Files ----- */
// IO
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm
#include <algorithm>
#include <cmath>
#include <numeric>

// container
#include <bitset>
#include <complex>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// others
#include <cassert>
#include <cstdint>
#include <ctime>
#include <functional>
#include <limits>
#include <random>

/* ----- Type Alias ----- */
using Bool = bool;
using Int = long long int;
using Real = long double;
using Char = char;
using String = std::string;
template <class T, class U> using Pair = std::pair<T, U>;
template <class... Ts> using Tuple = std::tuple<Ts...>;

template <class T> using Vector = std::vector<T>;
template <size_t N> using Bits = std::bitset<N>;
template <class T> using Queue = std::queue<T>;
template <class T> using Stack = std::stack<T>;
template <class T> using Deque = std::deque<T>;

template <class T> using MaxHeap = std::priority_queue<T>;
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T> using Set = std::set<T>;
template <class T, class U> using Map = std::map<T, U>;

template <class T, class... Us> using Func = std::function<T(Us...)>;

template <class T> T genv(T v) { return v; }

template <class T, class... Ts> auto genv(size_t l, Ts... ts) {
  return Vector<decltype(genv<T>(ts...))>(l, genv<T>(ts...));
}

template <class Cost = Int> struct Edge {
  Int src, dst;
  Cost cost;
  Edge(Int src = -1, Int dst = -1, Cost cost = 1)
      : src(src), dst(dst), cost(cost){};

  bool operator<(const Edge<Cost> &e) const { return this->cost < e.cost; }
  bool operator>(const Edge<Cost> &e) const { return this->cost > e.cost; }
};

template <class Cost = Int> using Edges = Vector<Edge<Cost>>;
template <class Cost = Int> using Graph = Vector<Vector<Edge<Cost>>>;

#endif

/* ----- Misc ----- */
void fastio() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
}

struct Fout {
  Int precision;
  Fout(Int precision) : precision(precision) {}
};
std::ostream &operator<<(std::ostream &os, const Fout &fio) {
  os << std::fixed << std::setprecision(fio.precision);
  return os;
}

/* ----- Constants ----- */
// constexpr Int INF = std::numeric_limits<Int>::max() / 3;
// constexpr Int MOD = 1000000007;
// constexpr Real PI = acos(-1);
constexpr Real EPS = 1e-10;
// std::mt19937 mt(int(std::time(nullptr)));

using Point = std::complex<Real>;
using Segment = std::pair<Point, Point>;
using Polygon = std::vector<Point>;

template <class T> T sq(T x) { return x * x; }

Bool lt_complex(const Point &a, const Point &b) {
  return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
}

std::istream &operator>>(std::istream &is, Point &p) {
  Real x, y;
  is >> x >> y;
  p = Point(x, y);
  return is;
}

// 宇宙船演算子
Int compare(Real a, Real b) {
  if (std::abs(a - b) < EPS)
    return 0;
  return a - b > 0 ? 1 : -1;
}

// outer product
Real cross(Point x, Point y) { return std::imag(std::conj(x) * (y)); }

Polygon convexhull(Polygon &g, Bool linearok) {
  std::sort(g.begin(), g.end(), lt_complex);
  Int N = g.size();

  Polygon ret(N * 2);
  Int k = 0;
  for (Int i = 0; i < N; ++i) {
    while (k >= 2 && compare(cross(ret[k - 1] - ret[k - 2], g[i] - ret[k - 2]),
                             0) < !linearok)
      --k;
    // linearokならcross = 0も回収する
    ret[k++] = g[i];
  }
  Int t = k + 1;
  for (Int i = N - 2; i >= 0; --i) {
    while (k >= t && compare(cross(ret[k - 1] - ret[k - 2], g[i] - ret[k - 2]),
                             0) < !linearok)
      --k;
    ret[k++] = g[i];
  }

  // 先頭が被っているので除く
  ret.resize(k - 1);
  return ret;
}

int main() {
  Int n;
  std::cin >> n;
  Polygon g;
  g.push_back(Point(0, 0));

  for (Int i = 0; i < n; ++i) {
    Point p;
    std::cin >> p;
    Polygon h;
    for (auto q : g)
      h.push_back(p + q);
    std::move(h.begin(), h.end(), std::back_inserter(g));
    g = convexhull(g, true);
  }

  Real ans = 0;
  for (auto p : g) {
    ans = std::max(ans, std::abs(p));
  }

  std::cout << Fout(20) << ans << std::endl;
  return 0;
}


Fixed submission:
#if __has_include("../library/Basic/Debug.hpp")

#include "../library/Basic/Debug.hpp"

#else

/* ----- Header Files ----- */
// IO
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm
#include <algorithm>
#include <cmath>
#include <numeric>

// container
#include <bitset>
#include <complex>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// others
#include <cassert>
#include <cstdint>
#include <ctime>
#include <functional>
#include <limits>
#include <random>

/* ----- Type Alias ----- */
using Bool = bool;
using Int = long long int;
using Real = long double;
using Char = char;
using String = std::string;
template <class T, class U> using Pair = std::pair<T, U>;
template <class... Ts> using Tuple = std::tuple<Ts...>;

template <class T> using Vector = std::vector<T>;
template <size_t N> using Bits = std::bitset<N>;
template <class T> using Queue = std::queue<T>;
template <class T> using Stack = std::stack<T>;
template <class T> using Deque = std::deque<T>;

template <class T> using MaxHeap = std::priority_queue<T>;
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T> using Set = std::set<T>;
template <class T, class U> using Map = std::map<T, U>;

template <class T, class... Us> using Func = std::function<T(Us...)>;

template <class T> T genv(T v) { return v; }

template <class T, class... Ts> auto genv(size_t l, Ts... ts) {
  return Vector<decltype(genv<T>(ts...))>(l, genv<T>(ts...));
}

template <class Cost = Int> struct Edge {
  Int src, dst;
  Cost cost;
  Edge(Int src = -1, Int dst = -1, Cost cost = 1)
      : src(src), dst(dst), cost(cost){};

  bool operator<(const Edge<Cost> &e) const { return this->cost < e.cost; }
  bool operator>(const Edge<Cost> &e) const { return this->cost > e.cost; }
};

template <class Cost = Int> using Edges = Vector<Edge<Cost>>;
template <class Cost = Int> using Graph = Vector<Vector<Edge<Cost>>>;

#endif

/* ----- Misc ----- */
void fastio() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
}

struct Fout {
  Int precision;
  Fout(Int precision) : precision(precision) {}
};
std::ostream &operator<<(std::ostream &os, const Fout &fio) {
  os << std::fixed << std::setprecision(fio.precision);
  return os;
}

/* ----- Constants ----- */
// constexpr Int INF = std::numeric_limits<Int>::max() / 3;
// constexpr Int MOD = 1000000007;
// constexpr Real PI = acos(-1);
constexpr Real EPS = 1e-10;
// std::mt19937 mt(int(std::time(nullptr)));

using Point = std::complex<Real>;
using Segment = std::pair<Point, Point>;
using Polygon = std::vector<Point>;

template <class T> T sq(T x) { return x * x; }

Bool lt_complex(const Point &a, const Point &b) {
  return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
}

std::istream &operator>>(std::istream &is, Point &p) {
  Real x, y;
  is >> x >> y;
  p = Point(x, y);
  return is;
}

// 宇宙船演算子
Int compare(Real a, Real b) {
  if (std::abs(a - b) < EPS)
    return 0;
  return a - b > 0 ? 1 : -1;
}

// outer product
Real cross(Point x, Point y) { return std::imag(std::conj(x) * (y)); }

Polygon convexhull(Polygon &g, Bool linearok) {
  std::sort(g.begin(), g.end(), lt_complex);
  Int N = g.size();

  Polygon ret(N * 2);
  Int k = 0;
  for (Int i = 0; i < N; ++i) {
    while (k >= 2 && compare(cross(ret[k - 1] - ret[k - 2], g[i] - ret[k - 2]),
                             0) < !linearok)
      --k;
    // linearokならcross = 0も回収する
    ret[k++] = g[i];
  }
  Int t = k + 1;
  for (Int i = N - 2; i >= 0; --i) {
    while (k >= t && compare(cross(ret[k - 1] - ret[k - 2], g[i] - ret[k - 2]),
                             0) < !linearok)
      --k;
    ret[k++] = g[i];
  }

  // 先頭が被っているので除く
  ret.resize(k - 1);
  return ret;
}

int main() {
  Int n;
  std::cin >> n;
  Polygon g;
  g.push_back(Point(0, 0));

  for (Int i = 0; i < n; ++i) {
    Point p;
    std::cin >> p;
    Polygon h;
    for (auto q : g)
      h.push_back(p + q);
    std::move(h.begin(), h.end(), std::back_inserter(g));
    g = convexhull(g, false);
  }

  Real ans = 0;
  for (auto p : g) {
    ans = std::max(ans, std::abs(p));
  }

  std::cout << Fout(20) << ans << std::endl;
  return 0;
}