CSES - Factory Machines
Author: Michael Cao
Appears In
In this problem, you're given machines and asked the minimum time these machines need to work to create products such that the -th machine creates a product in time.
Binary Search
Observe that the time needed to create at least products is monotonic. In other words, if the given machines can create products in time, then the same machines can create at least products in time. Using this property, we can binary search over the answer. Read this module for more information.
For some value we binary search for, , we are left with the task of checking whether we can create products in time. To do this, observe that it's optimal for every machine to work simultaneously. Then, in time, machine can create products.
Overall, the machines can create products. If this sum , then 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!