Cod sursa(job #1507816)

Utilizator PaulStighiStiegelbauer Paul-Alexandru PaulStighi Data 21 octombrie 2015 22:24:02
Problema Transport Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 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=0;    s+=V[i];    c++;    }
    }

    return (c<=K);
}

int main()
{
    int i,Sol;

    Left=1; Right=LM*LM;

    fin>>N>>K;

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

    while(Left<=Right)
    {
        Mid=(Left+Right)/2;

        if(check(Mid))  {   Right=Mid-1;  Sol=Mid+1;    }
        else    Left=Mid+1;
    }

    fout<<Sol<<"\n";

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