Factory Machines

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?

Example:

Input:  n = 3, t = 7, arr = {3, 2, 5}
Output: 8

Approach

C++

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;

bool check(long long midlong long t,
           long long nvector<long long&arr)
{
    long long cnt = 0;
    for (long long i = 0i < ni++)
    {
        cnt += mid / arr[i];
        if (cnt >= t)
            return true;
    }
    return false;
}

long long factoryMachines(long long nlong long t,
                          vector<long long&arr)
{

    long long low = 0high = INF;
    while (low + 1 < high)
    {
        long long mid = (low + high) / 2;
        if (check(midtnarr))
            high = mid;
        else
            low = mid;
    }
    return high;
}

int main()
{

    long long n = 3t = 7;

    vector<long longarr = {325};

    cout << factoryMachines(ntarr<< "\n";

    return 0;
}


No comments:

Post a Comment