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

#include <bits/stdc++.h>
using namespace std;
// 节点从0到n-1
#define maxn 100006
struct node {
  double x, y;
};
int n;        // 有多少个点
int tot;      // 凸包上的点
node p[maxn]; // 所有点数组从0开始
node P[maxn]; // 凸包上的点
const double pi = acos(-1.0);
// 三点差积
double X(node A, node B, node C) {
  return (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y);
}
// 两点距离
double len(node A, node B) {
  return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}
// 角度小的在前面,相同的话距离小的在前面
bool cmp(node A, node B) {
  double pp = X(p[0], A, B);
  if (pp > 0)
    return true;
  if (pp < 0)
    return false;
  if (pp == 0)
    return len(p[0], A) < len(p[0], B);
}
const double eps = 1e-9;
// 三点点积
double dian(node A, node B, node C) {
  return (B.x - A.x) * (C.x - A.x) + (C.y - A.y) * (B.y - A.y);
}
// 建立凸包存到P里//以下是n>=3的情况,n=1,2要特判
void build() {
  // 找左下角的点
  for (int i = 0; i < n; i++) {
    if (p[i].y < p[0].y)
      swap(p[i], p[0]);
    if (p[i].y == p[0].y && p[i].x < p[0].x)
      swap(p[i], p[0]);
  }
  sort(p + 1, p + n, cmp);
  P[0] = p[0];
  P[1] = p[1];
  tot = 1; // 初始两个点
  for (int i = 2; i < n; i++) {
    // 如果需要边上的点
    while (tot > 0 && X(P[tot - 1], P[tot], p[i]) < 0)
      tot--;
    //		while (tot > 0 && X(P[tot - 1], P[tot], p[i]) <=
    //0)tot--;//P[tot]在左边 舍弃
    tot++;
    P[tot] = p[i];
  }
  // 如果需要边上点 要这块
  node endd = P[tot];
  for (int i = n - 1; i >= 0; i--) {
    if (abs(X(P[0], endd, p[i])) <= eps) {
      if (dian(p[i], P[0], endd) < 0) {
        tot++;
        P[tot] = p[i];
      }
    } else
      break;
  }
  // 到这上面是需要注释掉的
  tot++;
}
int main() {
  //	freopen("a.txt","r",stdin);
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    cin >> p[i].x >> p[i].y;
  build();
  printf("%d\\n", tot);
  for (int i = 0; i < tot; i++) {
    cout << P[i].x << ' ' << P[i].y << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &i : v)
    is >> i;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  const string delimiter = "\\n";
  REP(i, v.size()) {
    os << v[i];
    if (i != v.size() - 1)
      os << delimiter;
  }
  return os;
}
/*--------------------template--------------------*/
const ld EPS = 1e-12;
typedef long double ld;
const ld PI = acos(-1.0);
bool eq(ld a, ld b) { return abs(a - b) < EPS; }
typedef complex<ld> Point;
typedef vector<Point> Polygon;

namespace std {
bool operator<(const Point &a, const Point &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std

struct Line {
  Point a, b;
  Line(){};
  Line(Point p, Point q) : a(p), b(q){};
  Line(ld x1, ld y1, ld x2, ld y2) : a(Point(x1, y1)), b(Point(x2, y2)){};
};

struct Circle {
  Point p;
  ld r;
  Circle(){};
  Circle(Point a, ld b) : p(a), r(b){};
};

ld dot(Point a, Point b) { return real(conj(a) * b); }

ld cross(Point a, Point b) { return imag(conj(a) * b); }

int ccw(Point a, Point b, Point c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return 1; // counter cloclwise
  if (cross(b, c) < -EPS)
    return -1; // cloclwise
  if (dot(b, c) < 0)
    return 2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;    // a--c--b on line
}

bool isis_ll(Line l, Line m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS; }

bool isis_ls(Line l, Line s) {
  return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;
}

bool isis_ss(Line s, Line t) {
  return (ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
          ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);
}

bool isis_lp(Line l, Point p) { return (abs(cross(l.b - p, l.a - p)) < EPS); }

bool isis_sp(Line s, Point p) {
  return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a)) < EPS;
}

Point projection(Line l, Point p) {
  Point base = l.b - l.a;
  ld r = dot(p - l.a, base) / norm(base);
  return l.a + base * r;
}

Point mirror(Line l, Point p) { return Point(2, 0) * projection(l, p) - p; }

ld dist_lp(Line l, Point p) { return abs(p - projection(l, p)); }

ld dist_ll(Line l, Line m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); }

ld dist_ls(Line l, Line s) {
  if (isis_ls(l, s))
    return 0;
  return min(dist_lp(l, s.a), dist_lp(l, s.b));
}

ld dist_sp(Line s, Point p) {
  Point r = projection(s, p);
  if (isis_sp(s, r))
    return abs(r - p);
  return min(abs(s.a - p), abs(s.b - p));
}

ld dist_ss(Line s, Line t) {
  if (isis_ss(s, t))
    return 0;
  return min(min(dist_sp(s, t.a), dist_sp(s, t.b)),
             min(dist_sp(t, s.a), dist_sp(t, s.b)));
}

Point is_ll(Line s, Line t) {
  ld a = cross(s.b - s.a, t.b - t.a);
  ld b = cross(s.b - s.a, s.b - t.a);
  return t.a + b / a * (t.b - t.a);
}

vector<Point> is_cc(Circle c1, Circle c2) {
  vector<Point> res;
  ld d = abs(c1.p - c2.p);
  ld rc = (d * d + pow(c1.r, 2) - pow(c2.r, 2)) / (2 * d);
  ld dfr = pow(c1.r, 2) - rc * rc;
  if (abs(dfr) < EPS)
    dfr = 0;
  if (dfr < 0.0)
    return res;
  ld rs = sqrt(dfr);
  Point diff = (c2.p - c1.p) / d;
  res.push_back(c1.p + diff * Point(rc, rs));
  if (dfr != 0.0)
    res.push_back(c1.p + diff * Point(rc, -rs));
  return res;
}

int isis_cc(Circle c1, Circle c2) {
  ld d = abs(c1.p - c2.p);
  if (d - c1.r - c2.r > EPS)
    return -2; // separate
  if (abs(d - c1.r - c2.r) < EPS)
    return -1; // circumscribe
  if (c1.r < c2.r)
    swap(c1, c2);
  if (abs(d - c1.r + c2.r) < EPS)
    return 1; // inscribe
  if (c1.r - d - c2.r > EPS)
    return 2; // involve
  else
    return 0; // intersect
}

bool isis_cl(Circle c, Line l) {
  ld d = dist_lp(l, c.p);
  return d - c.r < -EPS;
}

vector<Point> is_cl(Circle c, Line l) {
  vector<Point> res;
  ld d = dist_lp(l, c.p);
  if (d > c.r + EPS)
    return res;
  ld len = (d > c.r) ? 0 : sqrt(c.r * c.r - d * d);
  Point nor = (l.a - l.b) / abs(l.a - l.b);
  res.push_back(projection(l, c.p) + len * nor);
  if (abs(len) > EPS)
    res.push_back(projection(l, c.p - len * nor));
  return res;
}

vector<Point> is_cs(Circle c, Line s) {
  vector<Point> v = is_cl(c, s), res;
  REP(i, v.size()) {
    if (ccw(s.a, v[i], s.b) == -2)
      res.push_back(v[i]);
  }
  return res;
}

vector<Line> tangent_cp(Circle c, Point p) {
  vector<Line> res;
  Point v = c.p - p;
  ld d = abs(v);
  ld l = sqrt(norm(v) - c.r * c.r);
  if (isnan(l)) {
    return res;
  }
  Point v1 = v * Point(l / d, c.r / d);
  Point v2 = v * Point(l / d, -c.r / d);
  res.push_back(Line(p, p + v1));
  if (l < EPS)
    return res;
  res.push_back(Line(p, p + v2));
  return res;
}

vector<Line> tangent_cc(Circle c1, Circle c2) {
  vector<Line> res;
  if (abs(c1.p - c2.p) - (c1.r + c2.r) > -EPS) {
    Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);
    res = tangent_cp(c1, center);
  }
  if (abs(c1.r - c2.r) > EPS) {
    Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);
    vector<Line> nres = tangent_cp(c1, out);
    res.insert(res.end(), nres.begin(), nres.end());
  } else {
    Point v = c2.p - c1.p;
    v /= abs(v);
    Point q1 = c1.p + v * Point(0, 1) * c1.r;
    Point q2 = c1.p + v * Point(0, -1) * c1.r;
    res.push_back(Line(q1, q1 + v));
    res.push_back(Line(q2, q2 + v));
  }
  return res;
}

Polygon convex_hull(Polygon s) {
  Polygon u, l;
  if (s.size() < 3)
    return s;
  sort(ALL(s));
  u.push_back(s[0]);
  u.push_back(s[1]);
  l.push_back(s[s.size() - 1]);
  l.push_back(s[s.size() - 2]);
  FOR(i, 2, s.size()) {
    for (int n = u.size(); n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != -1; n--) {
      u.pop_back();
    }
    u.push_back(s[i]);
  }
  for (int i = s.size() - 3; i >= 0; i--) {
    for (int n = l.size(); n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) != -1; n--) {
      l.pop_back();
    }
    l.push_back(s[i]);
  }
  for (int i = u.size() - 2; i >= 1; i--)
    l.push_back(u[i]);
  return l;
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n;
  while (cin >> n, n) {
    vector<Point> v;
    REP(i, n) {
      ld x, y;
      cin >> x >> y;
      v.emplace_back(x, y);
    }
    Polygon a = convex_hull(v);
    cout << a.size() << endl;
    int s;
    Point p(11111, 11111);
    REP(i, a.size()) {
      if (a[i].imag() < p.imag()) {
        p = a[i];
        s = i;
      } else if (a[i].imag() == p.imag() && a[i].real() < p.real()) {
        p = a[i];
        s = i;
      }
    }
    REP(i, n) {
      cout << int(a[(i + s) % a.size()].real()) << " "
           << int(a[(i + s) % a.size()].imag()) << endl;
    }
  }
  return 0;
}