Cod sursa(job #2705561)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 12 februarie 2021 20:14:38
Problema Transport Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <fstream>
#include <vector>
#include <climits>

#define fi first
#define se second
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair< int , int > PII;

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

int n, k, st, dr = INT_MAX;
vector < int > V;

int check(int x) {
    int rs = 1, xx = x;

    for (auto it : V) {
        if (xx < it) {
            rs++;
            xx = x;
        }

        xx -= it;
    }

    return rs;
}

int main(){
    ios_base::sync_with_stdio(0);
    fin.tie(0); fout.tie(0);
 
    fin >> n >> k;
    for (int i = 1, x; i <= n; i++) {
        fin >> x;
        V.push_back(x);
        st = max(st, x);
    }

    int rs = 0;
    while (st <= dr) {
        int mid = (st + dr) >> 1;

        if (check(mid) <= k) {
            dr = mid - 1;
            rs = mid;
        } else {
            st = mid + 1;
        }
    }

    fout << rs;
    return 0;
}