Factory Machines CSES Solution | CSES Problem Set | Binary Search
Raunit Verma - in CSES
Views: 2
A factory has n machines which can be used to make products. Your goal is to make a total of t products. For each machine, you know the number of seconds it needs to make a single product. The machines can work simultaneously, and you can freely decide their schedule. What is the shortest time needed to make t products?
#include<bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main(){
int n, m;
cin >> n >> m;
vector<int> machines(n);
for(int i = 0; i < n; i++){
cin >> machines[i];
}
int low = 0, high = 1e18, mid;
while(low < high){
mid = low + (high - low) / 2;
int total_products = 0;
for(int i = 0; i < n; i++){
total_products += mid / machines[i];
if(total_products >= m) break;
}
if(total_products >= m){
high = mid;
} else {
low = mid + 1;
}
}
cout << low << endl;
return 0;
}Tagscsesfactory machinesfactory machines csescses solution
Raunit Verma
Technical Writer at CodingKaro
Share this on


