Cod sursa(job #1958646)

Utilizator cristicioteiCiotei Cristian cristiciotei Data 8 aprilie 2017 15:56:53
Problema Transport Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    freopen ("transport.in","r",stdin);
    freopen ("transport.out","w",stdout);
    int n,k,m,cm,l1,l2,s=0,max=-1,v[1001],ct=0,aux;
   cin>>n>>k;
   for (int i=1 ; i<=n ; i++)
   {
       cin>>v[i];
       if (max<v[i])
        max=v[i];
       s=s+v[i];
   }
   l1=max;l2=s;
   while (l1<=l2)
   {
       m=(l1+l2)/2;
       cm=m;ct=1;
       for (int z=1 ; z<=n ; z++)
       {
           if (m-v[z]>=0)
              m=m-v[z];
           else
           {
               ct++;
               m=cm;
               m=m-v[z];
           }

       }
       if (ct>k)
        l1=cm+1;
       else
       {
           aux=cm;
           l2=cm-1;
       }
   }
   cout<<aux;
    return 0;
}