Cod sursa(job #1777130)

Utilizator MithrilBratu Andrei Mithril Data 12 octombrie 2016 08:29:47
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <bits/stdc++.h>
using namespace std;
int n,k,best;
vector<int> V;
ifstream fin("transport.in");
ofstream fout("transport.out");

int main()
{
    fin>>n>>k;
    V.resize(n+1);
    V[0]=-1;
    int left0=INT_MIN,right0=0;
    for(int i=1;i<=n;i+=1){
        fin>>V[i];
        left0=max(left0,V[i]);
        right0+=V[i];
    }
    while(left0<=right0){
        int m=(left0+right0)/2;
        int attempts=1;
        int howMuchWeCarry=0;
        for(int i=1;i<=n;i+=1){
            howMuchWeCarry+=V[i];
            if(howMuchWeCarry>m){
                howMuchWeCarry=V[i];
                attempts+=1;
            }
        }
        if(attempts>k)
            left0=m+1;
        else{
            best=m;
            right0=m-1;
        }
    }
    fout<<best;
    return 0;
}