Problem p01508 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define sgn(v) (abs((v)) < eps ? 0 : ((v) < 0 ? -1 : 1))
#define sqr(v) ((v) * (v))
#define mb make_pair
#define debug 0
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<LL, LL> PLL;
const double eps = 1e-8;
const double pi = acos(-1);
const int maxn = 101;
struct Point {
  double x, y;
  Point() {}
  Point(double X, double Y) {
    x = X;
    y = Y;
  }
  bool operator<(const Point &p) const {
    if (sgn(x - p.x))
      return x < p.x;
    else
      return y < p.y;
  }
  bool operator==(const Point &p) const {
    return sgn(x - p.x) == 0 && sgn(y - p.y) == 0;
  }
  Point operator-(const Point &p) { return Point(x - p.x, y - p.y); }
  Point operator+(const Point &p) { return Point(x + p.x, y + p.y); }
  double cross(const Point &p) const { return x * p.y - y * p.x; }
  double len() { return sqrt(sqr(x) + sqr(y)); }
  void unit() {
    double tmp = len();
    x /= tmp;
    y /= tmp;
    x *= 0.0001;
    y *= 0.0001;
  }
} pt[maxn];
struct Segment {
  Point st, ed;
};
double cross(const Point &p, const Point &q) { return p.x * q.y - p.y * q.x; }
double dot(const Point &p, const Point &q) { return p.x * q.x + p.y * q.y; }
Point Insect(Segment &p, Segment &q) {
  double u = cross(p.ed - p.st, q.st - p.st),
         v = cross(p.st - p.ed, q.ed - p.st);
  return Point((q.st.x * v + q.ed.x * u) / (u + v),
               (q.st.y * v + q.ed.y * u) / (u + v));
}
vector<Point> tp, sp;
int n, cas;
int check(Point P, int deb) {
  int con = 0;
  Segment now, tmp;
  Point cp;
  now.st = P;
  con = 0;
  now.ed.x = P.x + 10000;
  19950527;
  now.ed.y = P.y + 10000;
  27051995;
  for (int i = 0; i < n; ++i) {
    tmp.st = pt[i];
    tmp.ed = pt[(i + 1) % n];
    if (sgn((tmp.ed - tmp.st).cross(now.ed - now.st))) {
      cp = Insect(tmp, now);
      if (cp == tmp.ed || cp == tmp.st)
        while (1)
          ;
      if (sgn(dot(tmp.ed - cp, cp - tmp.st)) > 0 &&
          sgn(dot(now.ed - cp, cp - now.st)) > 0)
        ++con;
    }
  }
  return con % 2;
}

double check(int i, int j) {
  Segment now, tmp;
  Point cp;
  tp.clear();
  now.st = pt[i];
  now.ed = pt[j];
  for (i = 0; i < n; ++i) {
    tmp.st = pt[i];
    tmp.ed = pt[(i + 1) % n];
    if (sgn((tmp.ed - tmp.st).cross(now.ed - now.st))) {
      cp = Insect(tmp, now);
      if (sgn(dot(tmp.ed - cp, cp - tmp.st)) > 0)
        tp.push_back(cp);
      else if (sgn(dot(tmp.ed - cp, cp - tmp.st)) == 0) // endpoints
      {
        if (cp == tmp.st)
          j = i;
        else
          j = i + 1;
        if (cross(pt[(j - 1 + n) % n] - now.st, now.ed - now.st) *
                cross(pt[(j + 1) % n] - now.st, now.ed - now.st) <
            0)
          tp.push_back(cp);
      }
    } else if (sgn((tmp.st - now.st).cross(now.ed - now.st)) == 0 &&
               sgn((tmp.ed - now.st).cross(now.ed - now.st)) == 0) {
      Point vec = tmp.ed - tmp.st;
      vec.unit();
      if (cross(pt[(i - 1 + n) % n] - now.st, now.ed - now.st) *
              cross(pt[(i + 2) % n] - now.st, now.ed - now.st) <
          0) {
        if (!check(tmp.st - vec, 0))
          tp.push_back(tmp.st);
        if (!check(tmp.ed + vec, 0))
          tp.push_back(tmp.ed);
      }
    }
  }
  sort(tp.begin(), tp.end());
  int n = tp.size();
  sp.clear();
  for (i = 0; i < n; ++i)
    if (i == 0 || (!(tp[i] == tp[i - 1])))
      sp.push_back(tp[i]);
  double ans = 0;
  for (i = 0; i < sp.size(); i += 2)
    ans = max(ans, (sp[i + 1] - sp[i]).len());
  return ans;
}
int main() {
  int i, j;
  cas = 0;
  while (scanf("%d", &n) != EOF) {
    if (n == 0)
      break;
    double len = 0;
    for (i = 0; i < n; ++i)
      scanf("%lf%lf", &pt[i].x, &pt[i].y);
    for (i = 0; i < n; ++i)
      len = max(len, (pt[i] - pt[(i + 1) % n]).len());
    for (i = 0; i < n; ++i)
      for (j = i + 1; j < n; ++j)
        len = max(len, check(i, j));
    printf("Case %d: %.10f\n", ++cas, len);
    // cout <<len*len<<endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define sgn(v) (abs((v)) < eps ? 0 : ((v) < 0 ? -1 : 1))
#define sqr(v) ((v) * (v))
#define mb make_pair
#define debug 0
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<LL, LL> PLL;
const double eps = 1e-8;
const double pi = acos(-1);
const int maxn = 101;
struct Point {
  double x, y;
  Point() {}
  Point(double X, double Y) {
    x = X;
    y = Y;
  }
  bool operator<(const Point &p) const {
    if (sgn(x - p.x))
      return x < p.x;
    else
      return y < p.y;
  }
  bool operator==(const Point &p) const {
    return sgn(x - p.x) == 0 && sgn(y - p.y) == 0;
  }
  Point operator-(const Point &p) { return Point(x - p.x, y - p.y); }
  Point operator+(const Point &p) { return Point(x + p.x, y + p.y); }
  double cross(const Point &p) const { return x * p.y - y * p.x; }
  double len() { return sqrt(sqr(x) + sqr(y)); }
  void unit() {
    double tmp = len();
    x /= tmp;
    y /= tmp;
    x *= 0.0001;
    y *= 0.0001;
  }
} pt[maxn];
struct Segment {
  Point st, ed;
};
double cross(const Point &p, const Point &q) { return p.x * q.y - p.y * q.x; }
double dot(const Point &p, const Point &q) { return p.x * q.x + p.y * q.y; }
Point Insect(Segment &p, Segment &q) {
  double u = cross(p.ed - p.st, q.st - p.st),
         v = cross(p.st - p.ed, q.ed - p.st);
  return Point((q.st.x * v + q.ed.x * u) / (u + v),
               (q.st.y * v + q.ed.y * u) / (u + v));
}
vector<Point> tp, sp;
int n, cas;
int check(Point P, int deb) {
  int con = 0;
  Segment now, tmp;
  Point cp;
  now.st = P;
  con = 0;
  now.ed.x = P.x + 19950527;
  now.ed.y = P.y + 27051995;
  for (int i = 0; i < n; ++i) {
    tmp.st = pt[i];
    tmp.ed = pt[(i + 1) % n];
    if (sgn((tmp.ed - tmp.st).cross(now.ed - now.st))) {
      cp = Insect(tmp, now);
      if (cp == tmp.ed || cp == tmp.st)
        while (1)
          ;
      if (sgn(dot(tmp.ed - cp, cp - tmp.st)) > 0 &&
          sgn(dot(now.ed - cp, cp - now.st)) > 0)
        ++con;
    }
  }
  return con % 2;
}

double check(int i, int j) {
  Segment now, tmp;
  Point cp;
  tp.clear();
  now.st = pt[i];
  now.ed = pt[j];
  for (i = 0; i < n; ++i) {
    tmp.st = pt[i];
    tmp.ed = pt[(i + 1) % n];
    if (sgn((tmp.ed - tmp.st).cross(now.ed - now.st))) {
      cp = Insect(tmp, now);
      if (sgn(dot(tmp.ed - cp, cp - tmp.st)) > 0)
        tp.push_back(cp);
      else if (sgn(dot(tmp.ed - cp, cp - tmp.st)) == 0) // endpoints
      {
        if (cp == tmp.st)
          j = i;
        else
          j = i + 1;
        if (cross(pt[(j - 1 + n) % n] - now.st, now.ed - now.st) *
                cross(pt[(j + 1) % n] - now.st, now.ed - now.st) <
            0)
          tp.push_back(cp);
      }
    } else if (sgn((tmp.st - now.st).cross(now.ed - now.st)) == 0 &&
               sgn((tmp.ed - now.st).cross(now.ed - now.st)) == 0) {
      Point vec = tmp.ed - tmp.st;
      vec.unit();
      if (cross(pt[(i - 1 + n) % n] - now.st, now.ed - now.st) *
              cross(pt[(i + 2) % n] - now.st, now.ed - now.st) <
          0) {
        if (!check(tmp.st - vec, 0))
          tp.push_back(tmp.st);
        if (!check(tmp.ed + vec, 0))
          tp.push_back(tmp.ed);
      }
    }
  }
  sort(tp.begin(), tp.end());
  int n = tp.size();
  sp.clear();
  for (i = 0; i < n; ++i)
    if (i == 0 || (!(tp[i] == tp[i - 1])))
      sp.push_back(tp[i]);
  double ans = 0;
  for (i = 0; i < sp.size(); i += 2)
    ans = max(ans, (sp[i + 1] - sp[i]).len());
  return ans;
}
int main() {
  int i, j;
  cas = 0;
  while (scanf("%d", &n) != EOF) {
    if (n == 0)
      break;
    double len = 0;
    for (i = 0; i < n; ++i)
      scanf("%lf%lf", &pt[i].x, &pt[i].y);
    for (i = 0; i < n; ++i)
      len = max(len, (pt[i] - pt[(i + 1) % n]).len());
    for (i = 0; i < n; ++i)
      for (j = i + 1; j < n; ++j)
        len = max(len, check(i, j));
    printf("Case %d: %.10f\n", ++cas, len);
    // cout <<len*len<<endl;
  }
  return 0;
}