63262023-11-18 15:46:38kukkermanBenzinkút üzemeltetés (55)cpp14Accepted 55/553ms4452 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using Benzinkutak = std::vector<std::pair<int, int>>;

void beolvas(std::istream &in, Benzinkutak &b, int &k) {
    int n;
    in >> n >> k;

    b.resize(n);
    for (auto i = 0; i < n; i++) {
        in >> b[i].first >> b[i].second;
    }
}

void feldolgoz(const Benzinkutak &b, int k) {
    const auto n = static_cast<int>(b.size());
    std::vector<std::pair<int, int>> dp(n + 1);
    
    dp[0] = { -1, 0 };
    for (auto i = 0; i < n; i++) {
        const auto elso_tiltott = std::upper_bound(b.begin(), b.end(), b[i].first - k, [](int tav, const auto &kut) {
            return tav < kut.first;
        }) - b.begin();

        const auto telepit_haszon = b[i].second + dp[elso_tiltott].second;
        const auto nem_telepit_haszon = dp[i].second;

        if (telepit_haszon < nem_telepit_haszon) {
            dp[i + 1] = { -1, nem_telepit_haszon };

        } else {
            dp[i + 1] = { static_cast<int>(elso_tiltott), telepit_haszon };
        }
    }

    std::vector<int> telepitett_indexek;
    auto i = n;
    while (i > 0) {
        if (dp[i].first >= 0) {
            telepitett_indexek.push_back(i);
            i = dp[i].first;

        } else {
            i--;
        }
    }

    std::cout << dp[n].second << std::endl;
    std::cout << telepitett_indexek.size() << ' ';
    for (auto ti = telepitett_indexek.crbegin(); ti != telepitett_indexek.crend(); ++ti) {
        std::cout << *ti << ' ';
    }
    std::cout << std::endl;
}

int main() {
    Benzinkutak b;
    int k;

    beolvas(std::cin, b, k);
    feldolgoz(b, k);

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/03ms1808 KiB
2Accepted0/03ms2076 KiB
3Accepted3/33ms2284 KiB
4Accepted3/33ms2484 KiB
5Accepted3/33ms2540 KiB
6Accepted3/33ms2760 KiB
7Accepted3/33ms2864 KiB
8Accepted3/33ms3072 KiB
9Accepted3/33ms3196 KiB
10Accepted3/33ms3440 KiB
11Accepted3/33ms3652 KiB
12Accepted3/33ms3872 KiB
13Accepted4/43ms4088 KiB
14Accepted4/43ms4300 KiB
15Accepted5/53ms4392 KiB
16Accepted6/63ms4452 KiB
17Accepted6/63ms4424 KiB