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

#include <cmath>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
using namespace std;

const double EPS = 1e-8;
const double INF = 1e12;
typedef complex<double> P, point;
typedef vector<P> polygon;
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); }

struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};
typedef L line;

P crosspoint(const L &l, const L &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < EPS && abs(B) < EPS)
    return m[0];
  return m[0] + B / A * (m[1] - m[0]);
}

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return +1; // counter clockwise
  if (cross(b, c) < 0)
    return -1; // clockwise
  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;
}

// !CAUTION! number ÍLÈã
#define curr(P, i) P[i]
#define next(P, i) P[(i + 1) % P.size()]
polygon convex_cut(const polygon &P, const line &l) {
  polygon Q;
  for (int i = 0; i < P.size(); ++i) {
    point A = curr(P, i), B = next(P, i);
    if (ccw(l[0], l[1], A) != -1)
      Q.push_back(A);
    if (ccw(l[0], l[1], A) * ccw(l[0], l[1], B) < 0)
      Q.push_back(crosspoint(line(A, B), l));
  }
  return Q;
}

double AreaOfPolygon(vector<P> p) {
  double S = 0;
  p.push_back(p[0]);
  /*@½p`ÌÊÏö® (½vñèÌê) */
  for (int i = 0; i < p.size() - 1; i++) {
    S += (p[i].real() - p[i + 1].real()) * (p[i].imag() + p[i + 1].imag());
  }
  S /= 2.0;
  return S;
}

double S = 0;

double getArea(vector<P> &g, P b) {
  return AreaOfPolygon(convex_cut(g, L(P(0, 0), b))) - S / 2;
}
int main() {
  srand(time(NULL));
  int N;
  while (cin >> N && N) {
    vector<P> g;
    for (int i = 0; i < N; i++) {
      double a, b;
      cin >> a >> b;
      g.push_back(P(a, b));
    }

    S = AreaOfPolygon(g);
    vector<double> o[2];
    bool done = 0;
    while (!o[0].size() || !o[1].size()) {
      double r = 2 * M_PI / 1000000000 * ((unsigned int)rand() % 1000000000);
      o[getArea(g, P(cos(r), sin(r))) > 0].push_back(r);
      if (fabs(getArea(g, P(cos(r), sin(r)))) < 1e-8) {
        printf("%.15lf %.15lf%c", cos(r) * 1000, sin(r) * 1000, 10);
        done = 1;
        break;
      }
      // cout << getArea(g, P(cos(r) , sin(r))) << " " << r << endl;
    }
    if (done)
      continue;
    double l = o[0][0], r = o[1][0];
    bool f = false;
    if (l > r)
      swap(l, r), f = 1;
    for (int _ = 0; _ < 128; _++) {
      double m = (l + r) / 2.0;
      if (getArea(g, P(cos(m), sin(m))) > 0) {
        if (f)
          l = m;
        else
          r = m;
      } else {
        if (f)
          r = m;
        else
          l = m;
      }
    }
    // cout << getArea(g,P(cos(l) , sin(l))) << endl;
    printf("%.15lf %.15lf%c", cos(l) * 1000, sin(l) * 1000, 10);
  exit:;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
using namespace std;

const double EPS = 1e-8;
const double INF = 1e12;
typedef complex<double> P, point;
typedef vector<P> polygon;
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); }

struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};
typedef L line;

P crosspoint(const L &l, const L &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < EPS && abs(B) < EPS)
    return m[0];
  return m[0] + B / A * (m[1] - m[0]);
}

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return +1; // counter clockwise
  if (cross(b, c) < 0)
    return -1; // clockwise
  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;
}

// !CAUTION! number ÍLÈã
#define curr(P, i) P[i]
#define next(P, i) P[(i + 1) % P.size()]
polygon convex_cut(const polygon &P, const line &l) {
  polygon Q;
  for (int i = 0; i < P.size(); ++i) {
    point A = curr(P, i), B = next(P, i);
    if (ccw(l[0], l[1], A) != -1)
      Q.push_back(A);
    if (ccw(l[0], l[1], A) * ccw(l[0], l[1], B) < 0)
      Q.push_back(crosspoint(line(A, B), l));
  }
  return Q;
}

double AreaOfPolygon(vector<P> p) {
  double S = 0;
  p.push_back(p[0]);
  /*@½p`ÌÊÏö® (½vñèÌê) */
  for (int i = 0; i < p.size() - 1; i++) {
    S += (p[i].real() - p[i + 1].real()) * (p[i].imag() + p[i + 1].imag());
  }
  S /= 2.0;
  return S;
}

double S = 0;

double getArea(vector<P> &g, P b) {
  return AreaOfPolygon(convex_cut(g, L(P(0, 0), b))) - S / 2;
}
int main() {
  srand(time(NULL));
  int N;
  while (cin >> N && N) {
    vector<P> g;
    for (int i = 0; i < N; i++) {
      double a, b;
      cin >> a >> b;
      g.push_back(P(a, b));
    }
    S = AreaOfPolygon(g);
    vector<double> o[2];
    while (!o[0].size() || !o[1].size()) {
      double r = 2 * M_PI / 1000000000 * ((unsigned int)rand() % 1000000000);
      o[getArea(g, P(cos(r), sin(r))) > 0].push_back(r);
      // cout << getArea(g, P(cos(r) , sin(r))) << " " << r << endl;
    }
    double l = o[0][0], r = o[1][0];
    bool f = false;
    if (l > r)
      swap(l, r), f = 1;
    for (int _ = 0; _ < 128; _++) {
      double m = (l + r) / 2.0;
      if (getArea(g, P(cos(m), sin(m))) > 0) {
        if (f)
          l = m;
        else
          r = m;
      } else {
        if (f)
          r = m;
        else
          l = m;
      }
    }
    // cout << getArea(g,P(cos(l) , sin(l))) << endl;
    printf("%.15lf %.15lf%c", cos(l) * 1000, sin(l) * 1000, 10);
  }
}