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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define INF 0x7FFFFFFF
const double EPS = 1e-10;

// -------
// -#-#---
// -##-#--
// --#----
// #------

inline bool eq(double a, double b) { return fabs(b - a) < EPS; }

struct p_t {
  double x;
  double y;
};
double cross(p_t p, p_t q);
p_t line(p_t p, p_t q);

bool asc(p_t left, p_t right) {
  return left.x == right.x ? left.y < right.y : left.x < right.x;
}

int main() {
  int n;
  p_t z;
  cin >> n;
  vector<p_t> a(n, z);
  vector<p_t> ab;
  vector<p_t> ac(n, z);
  vector<p_t> ad;
  vector<p_t> ae;
  for (int i = 0; i < n; i++) {
    cin >> a[i].x >> a[i].y;
  }
  sort(a.begin(), a.end(), asc);
  ac = a;
  reverse(ac.begin(), ac.end());
  ab.push_back(a[0]);
  ab.push_back(a[1]);
  for (int i = 2; i < (int)a.size(); i++) {
    while (ab.size() > 1 && cross(line(ab[ab.size() - 1], ab[ab.size() - 2]),
                                  line(ab[ab.size() - 1], a[i])) > 0) {
      ab.pop_back();
    }
    ab.push_back(a[i]);
  }
  ad.push_back(ac[0]);
  ad.push_back(ac[1]);
  for (int i = 2; i < (int)ac.size(); i++) {
    while (ad.size() > 1 && cross(line(ad[ad.size() - 1], ad[ad.size() - 2]),
                                  line(ad[ad.size() - 1], ac[i])) > 0) {
      ad.pop_back();
    }
    ad.push_back(ac[i]);
  }
  ab.pop_back();
  ab.insert(ab.end(), ad.begin(), ad.end());
  ab.pop_back();
  int w = 0;
  for (int i = 1; i < (int)ab.size(); i++) {
    if (ab[w].y == ab[i].y ? ab[w].x > ab[i].x : ab[w].y > ab[i].y) {
      w = i;
    }
  }
  for (int i = 0; i < (int)ab.size(); i++) {
    if ((i + w) >= (int)ab.size()) {
      w -= ab.size();
    }
    ae.push_back(ab[w + i]);
  }
  cout << (int)ab.size() << endl;
  for (int i = 0; i < (int)ab.size(); i++) {
    cout << ae[i].x << " " << ae[i].y << endl;
  }
}

p_t line(p_t p, p_t q) {
  p_t z;
  z.x = q.x - p.x;
  z.y = q.y - p.y;
  return z;
}

double cross(p_t p, p_t q) { return p.x * q.y - p.y * q.x; }


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define INF 0x7FFFFFFF
const double EPS = 1e-10;

// -------
// -#-#---
// -##-#--
// --#----
// #------

inline bool eq(double a, double b) { return fabs(b - a) < EPS; }

struct p_t {
  double x;
  double y;
};
double cross(p_t p, p_t q);
p_t line(p_t p, p_t q);

bool asc(p_t left, p_t right) {
  return left.x == right.x ? left.y < right.y : left.x < right.x;
}

int main() {
  int n;
  p_t z;
  cin >> n;
  vector<p_t> a(n, z);
  vector<p_t> ab(n, z);
  vector<p_t> ac(n, z);
  vector<p_t> ad(n, z);
  for (int i = 0; i < n; i++) {
    cin >> a[i].x >> a[i].y;
  }
  sort(a.begin(), a.end(), asc);
  ab = a;
  ac = a;
  reverse(ac.begin(), ac.end());
  int k = 1;
  while (k < (int)ab.size() - 1) {
    if (cross(line(ab[k], ab[k - 1]), line(ab[k], ab[k + 1])) > 0) {
      // cout<<"X "<<ab[k].x<< " "<<ab[k].y<<" "<<ab[k+1].x<< "
      // "<<ab[k+1].y<<endl;
      ab.erase(ab.begin() + k);
      if (k > 1) {
        k--;
      }
    } else {
      k++;
      // cout<<"O "<<ab[k-1].x<< " "<<ab[k-1].y<<endl;
    }
  }

  k = 1;
  while (k < (int)ac.size() - 1) {
    if (cross(line(ac[k], ac[k - 1]), line(ac[k], ac[k + 1])) > 0) {
      ac.erase(ac.begin() + k);
      if (k > 1) {
        k--;
      }
    } else {
      k++;
    }
  }
  ab.pop_back();
  ab.insert(ab.end(), ac.begin(), ac.end());
  ab.pop_back();
  int w = 0;
  for (int i = 1; i < (int)ab.size(); i++) {
    if (ab[w].y == ab[i].y ? ab[w].x > ab[i].x : ab[w].y > ab[i].y) {
      w = i;
    }
  }
  for (int i = 0; i < (int)ab.size(); i++) {
    if ((i + w) >= (int)ab.size()) {
      w -= ab.size();
    }
    ad[i] = ab[w + i];
  }
  cout << (int)ab.size() << endl;
  for (int i = 0; i < (int)ab.size(); i++) {
    cout << ad[i].x << " " << ad[i].y << endl;
  }
}

p_t line(p_t p, p_t q) {
  p_t z;
  z.x = q.x - p.x;
  z.y = q.y - p.y;
  return z;
}

double cross(p_t p, p_t q) { return p.x * q.y - p.y * q.x; }