Cod sursa(job #2786454)

Utilizator StefanL2005Stefan Leustean StefanL2005 Data 20 octombrie 2021 23:38:38
Problema Transport Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in ("transport.in");
ofstream out ("transport.out");
int numarare (int x, int n2, vector<int> z) {
    int ans = 0;
    for (int y = 1; y <= n2; y ++) {
        if (z[y] < x && z[y] != 0) {
            int S = z[y], l = y;
            while (S <= x) {
                l ++;
                S += z[l];
            }
            S -= z[l];
            l --;
            for (int y2 = y; y2 <= l; y2 ++)
                z[y2] = 0;
            ans++;
        }
        else
            if (z[y] == x)
                ans ++;
    }
    return ans;
}
int main()
{
    int n, k, Max, s = 0;
    in>> n >> k;
    vector<int> v(n + 1, 0);
    in>> v[1];
    Max = v[1];
    s += v[1];
    for (int i = 2; i <= n; i++) {
        in>> v[i];
        if (v[i] > Max)
            Max = v[i];
        s += v[i];
    }
    int c1 = Max, c2 = s, m;
    while (c2 - c1 > 1) {
        m = (c1 + c2) / 2;
        if (k >= numarare(m, n, v))
            c2 = m;
        else
            c1 = m;
    }
    if (numarare(c1, n, v) == k)
        out<< c1;
    else
        out<< c2;


    return 0;
}