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

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

typedef long long ll;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl

typedef vector<double> P;
const double eps = 1e-7;
struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};

P operator+(const P &a, const P &b) {
  P res;
  rep(i, 3) res.push_back(a[i] + b[i]);
  return res;
}
P operator-(const P &a, const P &b) {
  P res;
  rep(i, 3) res.push_back(a[i] - b[i]);
  return res;
}
double dot(P a, P b) {
  double res = 0;
  rep(i, 3) res += a[i] * b[i];
  return res;
}
double norm(P a) {
  double res = 0;
  rep(i, 3) res += a[i] * a[i];
  return res;
}
double abs(P a) { return sqrt(norm(a)); }
P projection(L l, P p) {
  P base = l[1] - l[0];
  double r = dot(p - l[0], base) / norm(base);
  P res;
  rep(i, 3) res.push_back(l[0][i] + base[i] * r);
  return res;
}
bool intersectSP(L s, P p) {
  return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < eps;
}
double distanceSP(L l, P p) {
  P r = projection(l, p);
  return intersectSP(l, r) ? abs(r - p) : min(abs(l[0] - p), abs(l[1] - p));
}

int main() {
  while (1) {
    int N, M, R;
    cin >> N >> M >> R;
    if (N == 0)
      break;
    vector<P> cps;
    vector<double> crs;
    rep(i, N) {
      P p;
      rep(j, 3) {
        double t;
        cin >> t;
        p.push_back(t);
      }
      cps.push_back(p);
      double r;
      cin >> r;
      crs.push_back(r);
    }
    vector<P> lps;
    vector<double> lbs;
    rep(j, M) {
      P p;
      rep(j, 3) {
        double t;
        cin >> t;
        p.push_back(t);
      }
      lps.push_back(p);
      double r;
      cin >> r;
      lbs.push_back(r);
    }
    P cent;
    rep(j, 3) {
      double t;
      cin >> t;
      cent.push_back(t);
    }
    vector<set<int>> idxs;
    vector<double> score;
    rep(i, M) {
      set<int> idx;
      rep(j, N) {
        double d = distanceSP(L(cent, lps[i]), cps[j]);
        if (d < crs[j] - eps && (abs(lps[i] - cps[j]) > crs[j] - eps ||
                                 abs(cent - cps[j]) > crs[j] - eps)) {
          idx.insert(j);
        }
      }
      idxs.push_back(idx);
      score.push_back(lbs[i] / norm(cent - lps[i]));
    }
    double res = -1e16;
    rep(S, 1 << M) {
      vector<bool> tot(N, 0);
      double sum = 0;
      int cnt = 0;
      rep(i, M) {
        if (!((S >> i) & 1))
          continue;
        for (int idx : idxs[i]) {
          if (!tot[idx])
            cnt++;
          tot[idx] = true;
          if (cnt > R)
            break;
        }
        sum += score[i];
        if (cnt > R)
          break;
      }
      if (cnt <= R) {
        res = max(res, sum);
      }
    }
    printf("%.10f\\n", res);
  }
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl

typedef vector<double> P;
const double eps = 1e-7;
struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};

P operator+(const P &a, const P &b) {
  P res;
  rep(i, 3) res.push_back(a[i] + b[i]);
  return res;
}
P operator-(const P &a, const P &b) {
  P res;
  rep(i, 3) res.push_back(a[i] - b[i]);
  return res;
}
double dot(P a, P b) {
  double res = 0;
  rep(i, 3) res += a[i] * b[i];
  return res;
}
double norm(P a) {
  double res = 0;
  rep(i, 3) res += a[i] * a[i];
  return res;
}
double abs(P a) { return sqrt(norm(a)); }
P projection(L l, P p) {
  P base = l[1] - l[0];
  double r = dot(p - l[0], base) / norm(base);
  P res;
  rep(i, 3) res.push_back(l[0][i] + base[i] * r);
  return res;
}
bool intersectSP(L s, P p) {
  return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < eps;
}
double distanceSP(L l, P p) {
  P r = projection(l, p);
  return intersectSP(l, r) ? abs(r - p) : min(abs(l[0] - p), abs(l[1] - p));
}

int main() {
  while (1) {
    int N, M, R;
    cin >> N >> M >> R;
    if (N == 0)
      break;
    vector<P> cps;
    vector<double> crs;
    rep(i, N) {
      P p;
      rep(j, 3) {
        double t;
        cin >> t;
        p.push_back(t);
      }
      cps.push_back(p);
      double r;
      cin >> r;
      crs.push_back(r);
    }
    vector<P> lps;
    vector<double> lbs;
    rep(j, M) {
      P p;
      rep(j, 3) {
        double t;
        cin >> t;
        p.push_back(t);
      }
      lps.push_back(p);
      double r;
      cin >> r;
      lbs.push_back(r);
    }
    P cent;
    rep(j, 3) {
      double t;
      cin >> t;
      cent.push_back(t);
    }
    vector<set<int>> idxs;
    vector<double> score;
    rep(i, M) {
      set<int> idx;
      rep(j, N) {
        double d = distanceSP(L(cent, lps[i]), cps[j]);
        if (d < crs[j] - eps && (abs(lps[i] - cps[j]) > crs[j] - eps ||
                                 abs(cent - cps[j]) > crs[j] - eps)) {
          idx.insert(j);
        }
      }
      idxs.push_back(idx);
      score.push_back(lbs[i] / norm(cent - lps[i]));
    }
    double res = -1e16;
    rep(S, 1 << M) {
      set<int> tot;
      double sum = 0;
      rep(i, M) {
        if (!((S >> i) & 1))
          continue;
        for (int idx : idxs[i]) {
          tot.insert(idx);
        }
        sum += score[i];
      }
      if (tot.size() <= R) {
        res = max(res, sum);
      }
    }
    printf("%.10f\\n", res);
  }
  return 0;
}