Cod sursa(job #2624797)

Utilizator RomanacheRoman Alexandru-George Romanache Data 5 iunie 2020 13:26:03
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <iostream>
#include <fstream>

using namespace std;

const int nmax=16000;
const int vmax=16000;
int v[nmax+1];
int n,k;

ifstream fin ("transport.in");
ofstream fout ("transport.out");

bool destul (int c)
{
    int nr=0,r=0;
    for (int i=0; i < n; i++)
    {
        if(v[i]>c)
        {
            return false;
        }
        if(v[i]>r)
        {
            nr++;
            r=c;
        }
        r-=v[i];
    }
    return (nr<=k);
}

int main()
{
    fin>>n>>k;
    for(int i=0; i<n; i++)
    {
        fin>>v[i];
    }
    int st=1,dr=vmax*nmax;
    while (st<dr)        /// cautbin la care adaugam functia bool
    {
        int m=(st+dr)/2;
        if (destul(m))
            dr=m;
        else
            st=m+1;
    }
    fout<< st;
    return 0;
}