Cod sursa(job #2537265)

Utilizator memecoinMeme Coin memecoin Data 3 februarie 2020 14:11:55
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <stack>

#define INF 0x3f3f3f3f

using namespace std;

#ifdef DEBUG
string name = "data";
#else
string name = "transport";
#endif

ifstream fin(name + ".in");
ofstream fout(name + ".out");

int n, k;
int a[16005];

int maxv = 0;

int transport(int v) {
    
    if (v < maxv) {
        return INF;
    }
    
    int t = 1;
    
    int s = 0;
    
    for (int i = 0; i < n; ++i) {
        s += a[i];
        
        if (s + a[i + 1] > v) {
            t++;
            s = 0;
        }
    }
    
    return t;
}


int find(int low, int high) {
    if (low < maxv) {
        return INF;
    }
    
    int mid = (low + high) / 2;
    
    auto t = transport(mid);
    
    if (t <= k && transport(mid - 1) > k) {
        return mid;
    } else if (t == k) {
        return find(low, mid - 1);
    }
    
    if (t < k) {
        return find(low, mid - 1);
    }
    
    return find(mid + 1, high);
}

int main() {
    
    fin >> n >> k;

    for (int i = 0; i < n; ++i) {
        fin >> a[i];
        maxv = max(maxv, a[i]);
    }
    
    fout << find(maxv, maxv * n);
}