Problem p03428 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define LOWER_BOUND(v, a) (lower_bound((v).begin(), (v).end(), (a)))
#define UPPER_BOUND(v, a) (upper_bound((v).begin(), (v).end(), (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
using namespace std;

int N;
int X[102];
int Y[102];

signed main() {
  cin >> N;
  REP(i, N) cin >> X[i] >> Y[i];

  if (N == 2) {
    cout << 0.5 << endl;
    cout << 0.5 << endl;
    return 0;
  }

  bool flg[100] = {};
  vector<int> pick;

  int min_i = -1;
  int min_x = INT_MAX;
  int min_y = INT_MAX;
  REP(i, N) {
    if (min_y == Y[i]) {
      if (min_x > X[i]) {
        min_x = X[i];
        min_i = i;
      }
    }
    if (min_y > Y[i]) {
      min_y = Y[i];
      min_i = i;
      min_x = X[i];
    }
  }

  int A = min_i;
  while (true) {
    pick.push_back(A);
    flg[A] = true;
    int B = 0;
    double max_len = 0;

    for (int i = 1; i < N; i++) {
      int C = i;

      int ab_x = X[B] - X[A];
      int ab_y = Y[B] - Y[A];
      int ac_x = X[C] - X[A];
      int ac_y = Y[C] - Y[A];

      double cross = ab_x * ac_y - ac_x * ab_y;

      int dx = ac_x - ab_x;
      int dy = ac_y - ab_y;

      double len = sqrt(ac_x * ac_x + ac_y * ac_y);
      if (cross > 0 || (cross == 0 && len > max_len)) {
        max_len = len;
        B = C;
      }
    }

    A = B;
    if (A == min_i)
      break;
  }

  cout << fixed << setprecision(10);

  if (pick.size() == 2) {
    REP(i, N) {
      if (flg[i])
        cout << "0.5" << endl;
      else
        cout << "0.0" << endl;
    }
    return 0.0;
  }

  double ans_list[100] = {};
  double center_x;
  double center_y;

  for (int i = 0; i < pick.size(); i++) {
    int p1 = pick[(i - 1 + pick.size()) % pick.size()];
    int p2 = pick[(i + 0) % pick.size()];
    int p3 = pick[(i + 1) % pick.size()];

    double vx1 = X[p2] - X[p1];
    double vy1 = Y[p2] - Y[p1];
    double vx2 = X[p3] - X[p2];
    double vy2 = Y[p3] - Y[p2];

    double dot = vx1 * vx2 + vy1 * vy2;
    double len1 = sqrt(vx1 * vx1 + vy1 * vy1);
    double len2 = sqrt(vx2 * vx2 + vy2 * vy2);
    double temp = dot / (len1 * len2);
    double angle = acos(temp);
    double ans = angle / (2 * M_PI);

    ans_list[pick[i]] = ans;
  }

  REP(i, N) { cout << ans_list[i] << endl; }
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define LOWER_BOUND(v, a) (lower_bound((v).begin(), (v).end(), (a)))
#define UPPER_BOUND(v, a) (upper_bound((v).begin(), (v).end(), (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
using namespace std;

int N;
int X[102];
int Y[102];

signed main() {
  cin >> N;
  REP(i, N) cin >> X[i] >> Y[i];

  if (N == 2) {
    cout << 0.5 << endl;
    cout << 0.5 << endl;
    return 0;
  }

  bool flg[100] = {};
  vector<int> pick;

  int min_i = -1;
  int min_x = INT_MAX;
  int min_y = INT_MAX;
  REP(i, N) {
    if (min_y == Y[i]) {
      if (min_x > X[i]) {
        min_x = X[i];
        min_i = i;
      }
    }
    if (min_y > Y[i]) {
      min_y = Y[i];
      min_i = i;
      min_x = X[i];
    }
  }

  int A = min_i;
  while (true) {
    pick.push_back(A);
    flg[A] = true;
    int B = 0;

    int ab_x0 = X[B] - X[A];
    int ab_y0 = Y[B] - Y[A];

    double max_len = sqrt(ab_x0 * ab_x0 + ab_y0 * ab_y0);
    ;

    for (int i = 1; i < N; i++) {
      int C = i;

      int ab_x = X[B] - X[A];
      int ab_y = Y[B] - Y[A];
      int ac_x = X[C] - X[A];
      int ac_y = Y[C] - Y[A];

      double cross = ab_x * ac_y - ac_x * ab_y;

      int dx = ac_x - ab_x;
      int dy = ac_y - ab_y;

      double len = sqrt(ac_x * ac_x + ac_y * ac_y);
      if (cross > 0 || (cross == 0 && len > max_len)) {
        max_len = len;
        B = C;
      }
    }

    A = B;
    if (A == min_i)
      break;
  }

  cout << fixed << setprecision(10);

  if (pick.size() == 2) {
    REP(i, N) {
      if (flg[i])
        cout << "0.5" << endl;
      else
        cout << "0.0" << endl;
    }
    return 0.0;
  }

  double ans_list[100] = {};
  double center_x;
  double center_y;

  for (int i = 0; i < pick.size(); i++) {
    int p1 = pick[(i - 1 + pick.size()) % pick.size()];
    int p2 = pick[(i + 0) % pick.size()];
    int p3 = pick[(i + 1) % pick.size()];

    double vx1 = X[p2] - X[p1];
    double vy1 = Y[p2] - Y[p1];
    double vx2 = X[p3] - X[p2];
    double vy2 = Y[p3] - Y[p2];

    double dot = vx1 * vx2 + vy1 * vy2;
    double len1 = sqrt(vx1 * vx1 + vy1 * vy1);
    double len2 = sqrt(vx2 * vx2 + vy2 * vy2);
    double temp = dot / (len1 * len2);
    double angle = acos(temp);
    double ans = angle / (2 * M_PI);

    ans_list[pick[i]] = ans;
  }

  REP(i, N) { cout << ans_list[i] << endl; }
}