Cod sursa(job #1677725)

Utilizator cyber_ghSoltan Gheorghe cyber_gh Data 6 aprilie 2016 19:13:47
Problema Transport Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream fin("transport.in");
ofstream fout("transport.out");
int n,k,a[16001],r,s,i,j,st,dr,piv,m;

int main(){
    int nrmax;
    fin>>n>>k;
    for(i=1;i<=n;i++) {fin>>a[i];
     if(a[i]>nrmax)
        {
            nrmax=a[i];
        }
    }
    st=1;
    dr=256000001;
    while(st<=dr){
     piv=(st+dr)/2;
     m=piv;
     s=0;
     r=1;
     for(i=1;i<=n;i++) {
         s+=a[i];
         if (s>m){
                 s=a[i];
             r++;
            
            }   
        }
        if (r<=k){
         dr=piv-1;   
        } 
        else 
        st=piv+1;
        
    }
    fout <<st;
    
    
 return 0;   
}