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

#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-10;
typedef complex<double> P;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
double cross(const P &a, const P &b) { return imag(conj(a) * b); }
double dot(const P &a, const P &b) { return real(conj(a) * b); }

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return +1;
  if (cross(b, c) < -EPS)
    return -1;
  if (dot(b, c) < -EPS)
    return +2;
  if (norm(b) < norm(c))
    return -2;
  return 0;
}

vector<P> convex_hull(vector<P> p) {
  int n = p.size(), k = 0;
  sort(p.begin(), p.end());
  vector<P> q(2 * n);
  for (int i = 0; i < n; q[k++] = p[i++])
    while (k >= 2 && ccw(q[k - 2], q[k - 1], p[i]) <= 0)
      --k;
  for (int i = n - 2, t = k + 1; i >= 0; q[k++] = p[i--])
    while (k >= t && ccw(q[k - 2], q[k - 1], p[i]) <= 0)
      --k;
  q.resize(k - 1);
  return q;
}

double area(vector<P> p) {
  double a = 0;
  for (int i = 0; i < (int)p.size(); i++)
    a += cross(p[i], p[(i + 1) % p.size()]);
  return fabs(a) / 2;
}

vector<P> v;
int n;
bool ck() {
  vector<P> a;
  double d[n];
  for (int i = 0; i < n; i++) {
    a.push_back(v[i]);
    d[i] = 0;
    if (a.size() < 3)
      continue;
    vector<P> b = convex_hull(a);
    d[i] = area(b);
    if (d[i] - d[i - 1] < EPS)
      return 0;
  }
  return 1;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    double x, y;
    cin >> x >> y;
    v.push_back(P(x, y));
  }
  for (int t = 0; t < 2; t++) {
    if (!ck()) {
      cout << "Impossible" << endl;
      return 0;
    }
    reverse(v.begin(), v.end());
  }
  cout << "Possible" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-10;
typedef complex<double> P;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
double cross(const P &a, const P &b) { return imag(conj(a) * b); }
double dot(const P &a, const P &b) { return real(conj(a) * b); }

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return +1;
  if (cross(b, c) < -EPS)
    return -1;
  if (dot(b, c) < -EPS)
    return +2;
  if (norm(b) < norm(c))
    return -2;
  return 0;
}

vector<P> convex_hull(vector<P> p) {
  int n = p.size(), k = 0;
  sort(p.begin(), p.end());
  vector<P> q(2 * n);
  for (int i = 0; i < n; q[k++] = p[i++])
    while (k >= 2 && ccw(q[k - 2], q[k - 1], p[i]) <= 0)
      --k;
  for (int i = n - 2, t = k + 1; i >= 0; q[k++] = p[i--])
    while (k >= t && ccw(q[k - 2], q[k - 1], p[i]) <= 0)
      --k;
  q.resize(k - 1);
  return q;
}

double area(vector<P> p) {
  double a = 0;
  for (int i = 0; i < (int)p.size(); i++)
    a += cross(p[i], p[(i + 1) % p.size()]);
  return fabs(a) / 2;
}

vector<P> v;
int n;
bool ck(int k) {
  vector<P> a;
  double d[n];
  for (int i = k; i < n; i++) {
    a.push_back(v[i]);
    d[i] = 0;
    if (a.size() < 3)
      continue;
    vector<P> b = convex_hull(a);
    d[i] = area(b);
    if (d[i] - d[i - 1] < EPS)
      return 0;
  }
  return 1;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    double x, y;
    cin >> x >> y;
    v.push_back(P(x, y));
  }
  for (int t = 0; t < 2; t++) {
    for (int i = 0; i < n; i++) {
      if (!ck(i)) {
        cout << "Impossible" << endl;
        return 0;
      }
    }
    reverse(v.begin(), v.end());
  }
  cout << "Possible" << endl;
  return 0;
}