Cod sursa(job #2674531)

Utilizator David861Retegan David David861 Data 19 noiembrie 2020 16:34:38
Problema Transport Scor 0
Compilator cpp-64 Status done
Runda simularecercazi Marime 1.13 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, k, c;
long long st, dr, mj;

bool checkVolume(int vol, int saltele[]) {
    int q = 0, dif, cdif, i = 1, cont = 0;
    
    while (i <= n) {
        dif = vol-saltele[i];
        if (dif <= 0) {
           q += 1;
           i++;
        } else {
            cdif = dif;
            cont = 0;
            while (cdif > 0) {
                i ++;
                cdif -= saltele[i];
                cont += 1;
            }
            if (cont > 1)
               q += 1;
        }
    }

   if (q > k)
       return false;
    else
       return true;
}

int main() {
    
    f >> n >> k;
    int s[n+1];
    s[0] = 0;
    for (int i = 1; i <= n; i++)
       f >> s[i];
      
       
    st = 1;
    dr = 256000000;
    
   while (st != dr) {
      mj = (st + dr)/2;
        if (checkVolume(mj, s) == true) {
          dr = mj;
        } else {
          st = mj + 1;
        }
        if (checkVolume(mj, s) == true)
           c = mj;
    }
    
    g << c;

    return 0;
}