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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int n, K;
  scanf("%d%d", &n, &K);

  vector<pair<double, double>> ps;
  const double PI = 3.14159265359;
  rep(i, 0, n) {
    double x, y, r;
    scanf("%lf%lf%lf", &x, &y, &r);
    if (y + r <= 0)
      continue;
    if (y - r <= 0) {
      double f, s;
      if (x < 0) {
        f = atan2(y, x) - asin(r / sqrt(x * x + y * y));
        s = PI;
      } else {
        f = 0;
        s = atan2(y, x) + asin(r / sqrt(x * x + y * y));
      }
      while (f < 0)
        f += PI;
      while (s < 0)
        s += PI;
      ps.push_back(mp(f, s));
    } else
      ps.push_back(mp(atan2(y, x) - asin(r / sqrt(x * x + y * y)),
                      atan2(y, x) + asin(r / sqrt(x * x + y * y))));
  }

  sort(ps.begin(), ps.end());
  ps.erase(unique(ps.begin(), ps.end()), ps.end());

  vector<pair<double, double>> tmp;
  rep(i, 0, ps.size()) {
    bool ok = true;
    rep(j, 0, ps.size()) {
      if (i == j)
        continue;
      if (ps[j].first <= ps[i].first && ps[i].second <= ps[j].second) {
        ok = false;
        break;
      }
    }
    if (ok)
      tmp.push_back(ps[i]);
  }
  ps = tmp;

  if (ps.size() == 0) {
    cout << 0 << endl;
    return;
  }

  vector<int> a(ps.size());
  rep(i, 1, ps.size()) {
    rep(j, 0, i + 1) {
      if (ps[j].second >= ps[i].first) {
        a[i] = j;
        break;
      }
    }
    if (a[i] == i)
      a[i] = i - 1;
  }

  static double dp[1500][1501][2];
  dp[0][0][0] = 0;
  dp[0][1][1] = ps[0].second - ps[0].first;
  double ans = dp[0][1][1];
  rep(i, 1, ps.size()) {
    rep(j, 0, K + 1) {
      dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1]);
      if (j == 0)
        continue;
      if (j == 1)
        dp[i][j][1] = ps[i].second - ps[i].first;
      dp[i][j][1] = max(dp[a[i]][j - 1][1] + ps[i].second -
                            max(ps[i].first, ps[a[i]].second),
                        dp[a[i]][j - 1][0] + ps[i].second - ps[i].first);
      ans = max({ans, dp[i][j][1], dp[i][j][0]});
    }
  }

  printf("%.10lf\\n", ans / PI);
}

int main() {
  Solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int n, K;
  scanf("%d%d", &n, &K);

  vector<pair<double, double>> ps;
  const double PI = 3.14159265359;
  rep(i, 0, n) {
    double x, y, r;
    scanf("%lf%lf%lf", &x, &y, &r);
    if (y + r <= 0)
      continue;
    if (y - r <= 0) {
      double f, s;
      if (x < 0) {
        f = atan2(y, x) - asin(r / sqrt(x * x + y * y));
        s = PI;
      } else {
        f = 0;
        s = atan2(y, x) + asin(r / sqrt(x * x + y * y));
      }
      while (f < 0)
        f += PI;
      while (s < 0)
        s += PI;
      ps.push_back(mp(f, s));
    } else
      ps.push_back(mp(atan2(y, x) - asin(r / sqrt(x * x + y * y)),
                      atan2(y, x) + asin(r / sqrt(x * x + y * y))));
  }

  sort(ps.begin(), ps.end());
  ps.erase(unique(ps.begin(), ps.end()), ps.end());

  vector<pair<double, double>> tmp;
  rep(i, 0, ps.size()) {
    bool ok = true;
    rep(j, 0, ps.size()) {
      if (i == j)
        continue;
      if (ps[j].first <= ps[i].first && ps[i].second <= ps[j].second) {
        ok = false;
        break;
      }
    }
    if (ok)
      tmp.push_back(ps[i]);
  }
  ps = tmp;

  if (ps.size() == 0) {
    cout << 0 << endl;
    return;
  }

  // vector<int> a(ps.size());
  // rep(i,1,ps.size()){
  // rep(j,0,i){
  // if(ps[j].second>=ps[i].first){
  // a[i]=j;
  // break;
  //}
  //}
  // if(a[i]==i) a[i]=i-1;
  //}

  static double dp[1500][1501][2];
  dp[0][0][0] = 0;
  dp[0][1][1] = ps[0].second - ps[0].first;
  double ans = dp[0][1][1];
  rep(i, 1, ps.size()) {
    rep(j, 0, K + 1) {
      dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1]);
      if (j == 0)
        continue;
      if (j == 1)
        dp[i][j][1] = ps[i].second - ps[i].first;
      rep(k, 0, i) {
        dp[i][j][1] = max(dp[i][j][1], dp[k][j - 1][1] + ps[i].second -
                                           max(ps[i].first, ps[k].second));
      }
      ans = max({ans, dp[i][j][1], dp[i][j][0]});
    }
  }

  printf("%.10lf\\n", ans / PI);
}

int main() {
  Solve();
  return 0;
}