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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, b, n) for (int i = b; i < n; i++)
#define RFOR(i, b, n) for (int i = n - 1; i >= b; i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define PI (acos(-1.0))
#define POSARG(a) (arg((a)) > 0.0 ? arg((a)) : 2.0 * PI + arg((a)))

using namespace std;

typedef complex<double> P;

int n, r, q, center; // center...回転の中心となる点のある添字
vector<P> vc;

P rotate_p(P a, P c, double t) {
  a = a - c;

  a = P(a.real() * cos(t) - a.imag() * sin(t),
        a.real() * sin(t) + a.imag() * cos(t));

  return a + c;
}

vector<P> rotate_vc(vector<P> vc, P c, double t) {
  for (int i = 0; i < (int)vc.size(); i++) {
    vc[i] = rotate_p(vc[i], c, t);
  }

  return vc;
}

void solve() {
  FOR(k, 0, q) {
    double upper = 2.0 * PI, lower = 0;

    FOR(j, 0, 50) {
      double mid = (upper + lower) / 2;
      vector<P> vc_r = rotate_vc(vc, vc[center], mid);
      bool out = false;

      FOR(i, 0, n) {
        if (abs(vc_r[i]) > r) {
          out = true;
        }
      }

      if (out) {
        upper = mid;
      } else {
        lower = mid;
      }
    }

    vector<P> vc_r = rotate_vc(vc, vc[center], lower);
    int newcenter = -1;

    FOR(i, 0, n) {
      if (i != center && EQ(abs(vc_r[i]), r)) {
        if (newcenter < 0 ||
            abs(vc_r[i] - vc[center]) > abs(vc_r[newcenter] - vc[center])) {
          newcenter = i;
        }
      }
    }

    printf("%.12lf %.12lf\\n", vc_r[newcenter].real(), vc_r[newcenter].imag());
    vc = vc_r;
    center = newcenter;
  }

  return;
}

int main() {
  while (cin >> n >> r >> q, (n || r || q)) {
    vc.clear();

    FOR(i, 0, n) {
      double x, y;
      cin >> x >> y;

      vc.push_back(P(x, y));

      if (EQ(x * x + y * y, r * r))
        center = i;
    }

    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, b, n) for (int i = b; i < n; i++)
#define RFOR(i, b, n) for (int i = n - 1; i >= b; i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define PI (acos(-1.0))
#define POSARG(a) (arg((a)) > 0.0 ? arg((a)) : 2.0 * PI + arg((a)))

using namespace std;

typedef complex<double> P;

int n, r, q, center; // center...回転の中心となる点のある添字
vector<P> vc;

P rotate_p(P a, P c, double t) {
  a = a - c;

  a = P(a.real() * cos(t) - a.imag() * sin(t),
        a.real() * sin(t) + a.imag() * cos(t));

  return a + c;
}

vector<P> rotate_vc(vector<P> vc, P c, double t) {
  for (int i = 0; i < (int)vc.size(); i++) {
    vc[i] = rotate_p(vc[i], c, t);
  }

  return vc;
}

void solve() {
  FOR(k, 0, q) {
    double upper = 2.0 * PI, lower = 0;

    FOR(j, 0, 100) {
      double mid = (upper + lower) / 2;
      vector<P> vc_r = rotate_vc(vc, vc[center], mid);
      bool out = false;

      FOR(i, 0, n) {
        if (abs(vc_r[i]) > r) {
          out = true;
        }
      }

      if (out) {
        upper = mid;
      } else {
        lower = mid;
      }
    }

    vector<P> vc_r = rotate_vc(vc, vc[center], lower);
    int newcenter = -1;

    FOR(i, 0, n) {
      if (i != center && EQ(abs(vc_r[i]), r)) {
        if (newcenter < 0 ||
            abs(vc_r[i] - vc[center]) > abs(vc_r[newcenter] - vc[center])) {
          newcenter = i;
        }
      }
    }

    printf("%.12lf %.12lf\\n", vc_r[newcenter].real(), vc_r[newcenter].imag());
    vc = vc_r;
    center = newcenter;
  }

  return;
}

int main() {
  while (cin >> n >> r >> q, (n || r || q)) {
    vc.clear();

    FOR(i, 0, n) {
      double x, y;
      cin >> x >> y;

      vc.push_back(P(x, y));

      if (EQ(x * x + y * y, r * r))
        center = i;
    }

    solve();
  }

  return 0;
}