Cod sursa(job #2624417)

Utilizator george_sdGeorge george_sd Data 4 iunie 2020 20:24:10
Problema Transport Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <fstream>

using namespace std;

ifstream f("transport.in");
ofstream g("transport.out");

int n, k, v[16001], mx, s;

int check(int c)
{
    int cc, ss=s, j=0;
    for(int i=0; i<k; i++)
    {
        cc=c;
        while(cc >= v[j])
        {
            cc-=v[j];
            s-=v[j++];
        }
    }
    return ss<=0;
}

int bin(int st, int dr) {
    int m;
    while(st<dr)
    {
        m=(st+dr)/2;
        if(check(m))
            dr=m;
        else
            st=m+1;
    }
    return st;
}

int main()
{
    f>>n>>k;
    for(int i=0; i<n; i++)
    {
        f>>v[i];
        s+=v[i];
        if(mx<v[i])
            mx=v[i];
    }
    g<<bin(mx, s);
    return 0;
}