Problem p01886 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < n; i++)
#define X first
#define Y second
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;

inline P operator+(const P &a, const P &b) { return P(a.X + b.X, a.Y + b.Y); }
inline P operator-(const P &a, const P &b) { return P(a.X - b.X, a.Y - b.Y); }
inline LL dot(P a, P b) { return a.X * b.X + a.Y * b.Y; }
inline LL cross(P a, P b) { return a.X * b.Y - a.Y * b.X; }

vector<LL> convex(vector<P> v) {
  int n = v.size(), k = 0;
  vector<LL> area(n + 1, 0);
  vector<P> a(n);
  FOR(i, n) {
    area[i + 1] = area[i];
    while (k > 1 && cross(a[k - 1] - a[k - 2], v[i] - a[k - 1]) < 0) {
      if (k >= 3)
        area[i + 1] -= cross(a[k - 2] - a[0], a[k - 1] - a[0]);
      k--;
    }
    a[k++] = v[i];
    if (k >= 3)
      area[i + 1] += cross(a[k - 2] - a[0], a[k - 1] - a[0]);
  }

  // for(auto i:area)cout<<i<<endl;

  return area;
}

vector<LL> CH[4];

int main() {
  int n;
  scanf("%d", &n);
  vector<P> p(n);
  FOR(i, n) scanf("%lld%lld", &p[i].X, &p[i].Y);

  sort(p.begin(), p.end());
  CH[0] = convex(p);
  FOR(i, n) p[i].Y *= -1;
  CH[2] = convex(p);
  FOR(i, n) p[i].X *= -1;
  sort(p.begin(), p.end());
  CH[3] = convex(p);
  FOR(i, n) p[i].Y *= -1;
  CH[1] = convex(p);
  FOR(i, n) p[i].X *= -1;
  sort(p.begin(), p.end());

  LL ans = CH[0][n] + CH[2][n] + CH[1][0] + CH[3][0];
  for (int i = 1; i < n; i++)
    if (p[i].X != p[i - 1].X)
      ans = min(ans, CH[0][i] + CH[2][i] + CH[1][n - i] + CH[3][n - i]);
  printf("%lld\\n", ans + 1 >> 1);
  return 0;
}

Error: TLE

Fixed submission:

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

struct Point {
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
};

typedef Point Vector;

Vector operator+(const Vector &A, const Vector &B) {
  return Vector(A.x + B.x, A.y + B.y);
}
Vector operator-(const Point &A, const Point &B) {
  return Vector(A.x - B.x, A.y - B.y);
}
double Cross(const Vector &A, const Vector &B) { return A.x * B.y - A.y * B.x; }

Vector Rotate(const Vector &A, double rad) {
  return Vector(A.x * cos(rad) - A.y * sin(rad),
                A.x * sin(rad) + A.y * cos(rad));
}

bool operator<(const Point &p1, const Point &p2) {
  return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y);
}

bool operator==(const Point &p1, const Point &p2) {
  return p1.x == p2.x && p1.y == p2.y;
}

vector<Point> ConvexHull(vector<Point> p) {
  int n = p.size();
  int m = 0;
  vector<Point> ch(n + 1);
  for (int i = 0; i < n; i++) {
    while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)
      m--;
    ch[m++] = p[i];
  }
  int k = m;
  for (int i = n - 2; i >= 0; i--) {
    while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)
      m--;
    ch[m++] = p[i];
  }
  if (n > 1)
    m--;
  ch.resize(m);
  return ch;
}

double PolygonArea(vector<Point> p) {
  double area = 0;
  int n = p.size();
  for (int i = 1; i < n - 1; i++)
    area += Cross(p[i] - p[0], p[i + 1] - p[0]);
  return area / 2;
}

vector<Point> p, p1, p2;

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i != n; ++i) {
    double x, y;
    scanf("%lf%lf", &x, &y);
    p.push_back(Point(x, y));
  }
  sort(p.begin(), p.end());
  int ans = PolygonArea(ConvexHull(p));
  for (int i = 0; i != n; ++i) {
    if (i != n - 1 && p[i].x == p[i + 1].x)
      continue;
    p1.clear();
    p2.clear();
    for (int j = 0; j <= i; ++j)
      p1.push_back(p[j]);
    for (int j = i + 1; j < n; ++j)
      p2.push_back(p[j]);
    double area = PolygonArea(ConvexHull(p1)) + PolygonArea(ConvexHull(p2));
    int area2 = (int)(area + 0.5);
    ans = min(ans, area2);
  }
  printf("%d\\n", ans);
}