Cod sursa(job #3279281)

Utilizator mlupseLupse-Turpan Mircea mlupse Data 22 februarie 2025 13:20:05
Problema Transport Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
using namespace std;
ifstream fin("transport.in");
ofstream fout("transport.out");
const int NMax = 16000;
int X[NMax + 5],N,K,C;
int Check(int Value)
{
    int Count = 0,S = 0;
    for(int i = 1; i <= N; ++i)
    {
        if(S + X[i] <= Value)
            S += X[i];
        else
        {
            S = X[i];
            Count++;
        }
    }
    Count++;
    return (Count <= K);
}
int main()
{
    fin >> N >> K;
    for(int i = 1; i <= N; ++i)
        fin >> X[i];
    int Left = 1, Right = 16000*16000;
    int Sol = -1;
    while(Left <= Right)
    {
        int Mid = (Left + Right) / 2;
        if(Check(Mid))
        {
            Sol = Mid;
            Right = Mid - 1;

        }
        else
           Left = Mid + 1;
    }
    fout << Sol << "\n";
    return 0;
}