P1594: Convoy — Solution
Approach
One look at this problem and you know it is a DP that partitions the vehicles. At first I thought of F[first i vehicles][number of partitions j] = the time for the first i vehicles split j times, then found this formulation is very slow; besides, this problem does not say how many partitions there are, and we only need the solution for the first n vehicles.
Transition equation: F[i] = F[j-1] + L / (lowest_spd in [1,j]), and the transition is allowed only when there is no overload.
This problem is pretty nasty: int will get caught out, and the infinity for double has to be a True·Infinity.
Code
// P1594.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#define ll long long
const double INF = 9223372036854775807ll;
const ll maxn = 1100;
struct car
{
double v, w;
} cars[maxn];
double W, L, N;
double F[maxn];
void DP()
{
F[0] = 0;
for (ll i = 1; i <= N; i++)
{
F[i] = (double)INF;
double load = 0;
double lspd = INF;
for (ll j = i; j > 0; j--)
{
load += cars[j].w;
if (load > W)
break;
lspd = min(lspd, cars[j].v);
F[i] = min(F[i], F[j - 1] + L / lspd);
}
}
}
int main()
{
cin >> W >> L >> N;
for (ll i = 1; i <= N; i++)
cin >> cars[i].w >> cars[i].v;
DP();
cout << fixed << setprecision(1) << F[(ll)N] * 60;
return 0;
}