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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
        for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
                if (i) os << " ";
                os << get<0>(t);
        }
        return os;
}
*/
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

int n;
int pw;
vector<int> vw, pf, vf, th;
double f(double w) {
  double res = w * pw;
  REP(i, n) {
    double v = vw[i] * w;
    if (v < th[i]) {
      double F = (th[i] - v) / vf[i];
      res += F * pf[i];
    }
  }
  return res;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout << fixed << setprecision(10);
  while (cin >> n, n) {
    vw.resize(n);
    pf.resize(n);
    vf.resize(n);
    th.resize(n);
    cin >> pw;
    REP(i, n) { cin >> vw[i] >> pf[i] >> vf[i] >> th[i]; }
    // vector<double> kh; kh.pb(0);
    // REP(i, n) {
    //     if (abs(vw[i]) > 0) {
    //         kh.pb(abs((double)th[i]/vw[i]));
    //     }
    // }
    // double ans = 0;
    // REP(i, n) {
    //     if (th[i] > 0) {
    //         ans += th[i]/vf[i]*pf[i];
    //     }
    // }
    // double d = 0;
    // double ans = f(0);
    // sort( ALL(kh) );
    // REP(i, (int)kh.size()) {
    //     ans = min(ans, f(kh[i]));
    // }
    // kh.pb(linf);
    double l = 0, r = linf;
    REP(t, 200) {
      double m1 = l + (r - l) / 3;
      double m2 = l + (r - l) / 3 * 2;
      if (f(m1) < f(m2)) {
        r = m2;
      } else {
        l = m1;
      }
    }
    // cout << l <<  " " << r << endl;
    double ans = f(l);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
        for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
                if (i) os << " ";
                os << get<0>(t);
        }
        return os;
}
*/
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

int n;
int pw;
vector<int> vw, pf, vf, th;
double f(double w) {
  double res = w * pw;
  REP(i, n) {
    double v = vw[i] * w;
    if (v < th[i]) {
      double F = (th[i] - v) / vf[i];
      res += F * pf[i];
    }
  }
  return res;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout << fixed << setprecision(10);
  while (cin >> n, n) {
    vw.resize(n);
    pf.resize(n);
    vf.resize(n);
    th.resize(n);
    cin >> pw;
    REP(i, n) { cin >> vw[i] >> pf[i] >> vf[i] >> th[i]; }
    vector<double> kh;
    kh.pb(0);
    REP(i, n) {
      if (abs(vw[i]) > 0) {
        kh.pb(abs((double)th[i] / vw[i]));
      }
    }
    // double ans = 0;
    // REP(i, n) {
    //     if (th[i] > 0) {
    //         ans += th[i]/vf[i]*pf[i];
    //     }
    // }
    // double d = 0;
    double ans = f(0);
    sort(ALL(kh));
    REP(i, (int)kh.size()) { ans = min(ans, f(kh[i])); }
    kh.pb(linf);
    // double l = 0, r = linf;
    // REP(t, 100) {
    //     double m1 = (l + 2 * r)/3;
    //     double m2 = (2 * l + r)/3;
    //     if ( f(m1) < f(m2) ) {
    //         l = m1;
    //     }
    //     else {
    //         r = m2;
    //     }
    // }
    // ans = f(l);
    cout << ans << endl;
  }
}