1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| const int N = 1001; const double eps = 1e-8, INF = INT_MAX; double f[N][N]; class Solution { public: int minSkips(vector<int>& dist, int speed, int hoursBefore) { int n = dist.size(); for (int i = 1; i <= n; ++i) { double t = (double) dist[i - 1] / speed; for (int j = 0; j <= i; ++j) { f[i][j] = INF; if (j <= i - 1) f[i][j] = ceil(f[i - 1][j] + t - eps); if (j) f[i][j] = min(f[i][j], f[i - 1][j - 1] + t); } } for (int i = 0; i <= n; ++i) if (f[n][i] < hoursBefore + eps) return i; return -1; } };
|