Cod sursa(job #2467778)

Utilizator CraiuAndrei Craiu Craiu Data 5 octombrie 2019 04:28:32
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>
#define nmax 16005

using namespace std;

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

int n, k;
int a[nmax];

int main()
{
    int i, s, cmax, st, dr, mij, suma, cnt, sol;
    fin >> n >> k;
    s = cmax = 0;
    for(i = 1; i <= n; i++)
    {
        fin >> a[i];
        s += a[i];
        cmax = max(a[i], cmax);
    }
    st = cmax;
    dr = s;
    while(st <= dr)
    {
        mij = (st + dr) / 2;
        suma = 0;
        cnt = 1;
        for(i = 1; i <= n; i++)
        {
            suma += a[i];
            if(suma > mij)
            {
                suma = a[i];
                cnt++;
            }
        }
        if(cnt > k) st = mij + 1;
        else
        {
            sol = mij;
            dr = mij - 1;
        }
    }
    fout << sol <<"\n";
    fout.close();
    return 0;
}