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-picture
Raunit Verma

Technical Writer at CodingKaro

Share this on
CodingKaro App Poster
CodingKaro App
4.7

3K+ Downloads

One of its unique features is an automated coding contest reminder, which sets alarms for upcoming coding contests on popular platforms like CodeChef, CodeForces, and LeetCode, without the need for user input. This feature ensures that users never miss a coding contest and can stay updated with the latest coding challenges.

Download CodingKaro AppDownload CodingKaro App
Other Blogs in CSES