Cod sursa(job #1509658)

Utilizator PaulStighiStiegelbauer Paul-Alexandru PaulStighi Data 24 octombrie 2015 10:43:07
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include<fstream>
#define LM 16008
using namespace std;
ifstream fin("transport.in");
ofstream fout("transport.out");

int V[LM],N,K,Left,Right,Mid;

int check(int als)
{
    int s=0,c=0,i;

    for(i=1;i<=N;++i)
    {
        if(s+V[i]<=als)     s+=V[i];
        else    {   s=V[i];    c++;    }
    }

    return (c+1<=K);
}

int main()
{
    int i,Sol,m=0;

    fin>>N>>K;

    for(i=1;i<=N;++i)
    {
        fin>>V[i];
        m=max(m,V[i]);
    }

    Left=m; Right=LM*LM;
    if(K==N)    Sol=m;
    else
    {   Mid=(Left+Right)/2;
        while(Left<Right)
        {
            if(check(Mid))  {   Right=Mid;  Sol=Mid;    }
            else    Left=Mid+1;

            Mid=(Left+Right)/2;
        }
    }

    fout<<Sol<<"\n";

    fin.close();
    fout.close();
    return 0;
}