Cod sursa(job #185169)

Utilizator stef2nStefan Istrate stef2n Data 24 aprilie 2008 20:40:37
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>
using namespace std;

const int MAX_N = 16000;
const int MAX_V = 16000;

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

int N, K, V[MAX_N];

void read() {
    in >> N >> K;
    for(int i = 0; i < N; ++i)
        in >> V[i];
}

int max_value() {
    int v = 0;
    for(int i = 0; i < N; ++i)
        v = max(v, V[i]);
    return v;
}

bool is_possible(int C) {
    int cnt = 0, last = 0;
    for(int i = 0; i < N; ++i)
        if(last + V[i] <= C)
            last += V[i];
        else
            ++cnt, last = V[i];
    if(last)
        ++cnt;
    return cnt <= K;
}

int binary_search(int lo, int hi) {
    if(lo == hi)
        return lo;
    if(is_possible((lo + hi) / 2))
        return binary_search(lo, (lo + hi) / 2);
    else
        return binary_search((lo + hi) / 2 + 1, hi);
}


int main() {
    read();
    out << binary_search(max_value(), MAX_N * MAX_V) << endl;
    return 0;
}