Problem p01704 (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 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 infll = 1LL << 62;
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;
}

int myrand() {
  static random_device rd;
  static mt19937 eng(rd());
  return eng();
}

double pw, vw[100000], pf[100000], vf[100000], th[100000];

void solve(int n) {
  scanf("%lf", &pw);
  rep(i, 0, n) scanf("%lf%lf%lf%lf", vw + i, pf + i, vf + i, th + i);

  vector<double> v = {0};
  rep(i, 0, n) if (vw[i] != 0 and th[i] / vw[i] >= 0)
      v.push_back(th[i] / vw[i]);
  // sort(v.begin(),v.end());
  // rep(i,0,v.size()-1) if(v[i+1]-v[i]<=1e-5) v[i+1]=v[i];
  // v.erase(unique(v.begin(),v.end()),v.end());

  double ans = inf;
  for (double w : v) {
    if (v.size() >= 8000 and myrand() % 5 == 0)
      continue;
    double d = w * pw;
    rep(i, 0, n) {
      if (d >= ans)
        break;
      d += pf[i] * max(0., (th[i] - w * vw[i]) / vf[i]);
    }
    ans = min(ans, d);
  }
  printf("%f\\n", ans);
}

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (!n)
      break;
    solve(n);
  }
  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 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 infll = 1LL << 62;
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;
}

int myrand() {
  static random_device rd;
  static mt19937 eng(rd());
  return eng();
}

double pw, vw[100000], pf[100000], vf[100000], th[100000];

void solve(int n) {
  scanf("%lf", &pw);
  rep(i, 0, n) scanf("%lf%lf%lf%lf", vw + i, pf + i, vf + i, th + i);

  vector<double> v = {0};
  rep(i, 0, n) if (vw[i] != 0 and th[i] / vw[i] >= 0)
      v.push_back(th[i] / vw[i]);
  sort(v.begin(), v.end());
  rep(i, 0, v.size() - 1) if (v[i + 1] - v[i] <= 1e-5) v[i + 1] = v[i];
  v.erase(unique(v.begin(), v.end()), v.end());

  double ans = inf;
  for (double w : v) {
    if (v.size() >= 10000 and myrand() % 4 == 0)
      continue;
    double d = w * pw;
    rep(i, 0, n) {
      if (d >= ans)
        break;
      d += pf[i] * max(0., (th[i] - w * vw[i]) / vf[i]);
    }
    ans = min(ans, d);
  }
  printf("%f\\n", ans);
}

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (!n)
      break;
    solve(n);
  }
  return 0;
}