Cod sursa(job #856038)

Utilizator alexalghisiAlghisi Alessandro Paolo alexalghisi Data 15 ianuarie 2013 21:55:22
Problema Transport Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <fstream>
#define DN  16005
using namespace std;

int n,k,v[DN];

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


int trucks(int max)
{
    int i=1,rounds=0,S=0;
    while(i<=n)
    {
        if(v[i]>max)
            return 1<<30;
        if(S+v[i]>max)
            {
                S=v[i];
                ++rounds;
            }
        else
            S+=v[i];
        ++i;
    }
    if(S)
        ++rounds;
   // cout<<max<<" "<<rounds<<endl;
    return rounds;
}

void caut_bin()
{
    int li=0,ls=k*DN,mij,rez;
    while(li<=ls)
    {
        mij=(li+ls)/2;
        if( trucks(mij) <=k )
        {
            rez=mij;
            ls=mij-1;
        }
        else
            li=mij+1;
    }
    g<<rez;
}

int main()
{
    f>>n>>k;
    for(int i=1;i<=n;++i)
        f>>v[i];
    caut_bin();
    return 0;
}