Cod sursa(job #2955221)

Utilizator Pop_EmilPal Tamas Pop_Emil Data 16 decembrie 2022 16:27:01
Problema Transport Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <iostream>
using namespace std;

FILE *in = fopen("transport.in", "r"), *out = fopen("transport.out", "w");
int N, K, s[16005], maxS;
const int maxC = 256000000;
//const int maxC = 100;

void _read() {
    fscanf(in, "%d %d", &N, &K);
    for (int i = 1; i <= N; ++i) {
        fscanf(in, "%d", &s[i]);
        maxS = max(maxS, s[i]);
    }
}

int nr_of_transports(int C) {
    int res = 0, truck = 0;
    for(int i = 1; i <= N; ++i){
        if (truck + s[i] <= C)
            truck += s[i];
        else {
            ++res;
            truck = s[i];
        }
    }
    if (truck > 0)
        ++res;
    return res;
}

int BS(int L, int R) {

    int M, valM;
    while (L <= R) {
        M = (L + R) / 2;
        valM = nr_of_transports(M);
        if (valM < K)
            R = M - 1;
        else if (valM > K)
            L = M + 1;
        else {
            while (M - 1 >= L && nr_of_transports(M - 1) == K)
                --M;
            return M;
        }
    }
    return L;
}

int main()
{
    _read();

    int C;
    C = BS(maxS, maxC);

    fprintf(out, "%d", C);
    return 0;
}