Cod sursa(job #2366659)

Utilizator AlexHocHociota Alexandru AlexHoc Data 4 martie 2019 21:27:05
Problema Transport Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <iostream>
#include <fstream>
using namespace std;
int v[16001];
int main(){
    ifstream fin("transport.in");
    ofstream fout("transport.out");
    int i,N,K,st,dr=0,h,solutie,n,p;
    fin>>N>>K;
    for(i=1;i<=N;i++){
        fin>>v[i];
        dr+=v[i];
    }
    st=v[1];
    solutie=1000000;
    int cdr=dr;
    int cst=st;
    while(st<=dr && st>=cst && dr>=cst){
        h=1;
        p=0;
        n=(st+dr)/2;
        for(i=1;i<=N;i++){
            p+=v[i];
            if(p>n){
                p=0;
                p+=v[i];
                h++;
            }
        }
        if(h<=K && n<solutie){
            solutie=n;
            dr=n-1;
        }
        else
            st=n+1;
        
    }
    fout<<solutie;
    return 0;
}