CSES - Factory Machines

Author: Michael Cao

In this problem, you're given nn machines and asked the minimum time these machines need to work to create tt products such that the ii-th machine creates a product in kik_i time.

Binary Search

Observe that the time needed to create at least xx products is monotonic. In other words, if the given machines can create xx products in y1y_1 time, then the same machines can create at least xx products in y2>y1y_2 > y_1 time. Using this property, we can binary search over the answer. Read this module for more information.

For some value we binary search for, ansans, we are left with the task of checking whether we can create tt products in ansans time. To do this, observe that it's optimal for every machine to work simultaneously. Then, in ansans time, machine ii can create anski\lfloor \frac{ans}{k_i} \rfloor products.

Overall, the nn machines can create ‎‎i=1nanski\sum_{i=1}^{n} \lfloor \frac{ans}{k_i} \rfloor products. If this sum t\geq t, then ansans is valid.

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define f first

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!