Cod sursa(job #2427975)

Utilizator costin2909Apostu Alexandru costin2909 Data 3 iunie 2019 10:32:12
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <iostream>
#include <fstream>

using namespace std;

const int VMAX=16000*16000;

const int NMAX=16000;

int n,k,v[NMAX];

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

int main()
{
    fstream fin("transport.in");
    ofstream fout("transport.out");
    fin>>n>>k;
    for(int i=0; i<n; i++)
    {
        fin>>v[i];
    }
    int st=1;
    int dr=VMAX;
    while(st<dr)
    {
        int m=(st+dr)/2;
        if(suficient(m))
        {
            dr=m;
        }
        else
        {
            st=m+1;
        }
    }
    fout<<st;
    fin.close();
    fout.close();
    return 0;
}