60032023-10-15 12:31:13CattBenzinkút üzemeltetés (55)cpp17Wrong answer 12/554ms3976 KiB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct RestStop {
    int distance;
    int profit;
};

int main() {
    int N, K;
    cin >> N >> K;

    vector<RestStop> restStops(N);
    for (int i = 0; i < N; i++) {
        cin >> restStops[i].distance >> restStops[i].profit;
    }

    // Sorrendezzük a pihenőhelyeket a távolság alapján.
    sort(restStops.begin(), restStops.end(), [](const RestStop &a, const RestStop &b) {
        return a.distance < b.distance;
    });

    vector<long long> dp(N);
    vector<int> previous(N, -1);

    // Dinamikus programozás: dp[i] tárolja a maximális hasznot az i. pihenőhelyig.
    for (int i = 0; i < N; i++) {
        dp[i] = restStops[i].profit;

        for (int j = 0; j < i; j++) {
            int distance = restStops[i].distance - restStops[j].distance;

            if (distance >= K) {
                dp[i] = max(dp[i], dp[j] + restStops[i].profit);
                previous[i] = j;
            }
        }
    }

    // Keresés a maximális haszonhoz és az épített benzinkutak helyéhez.
    long long maxProfit = *max_element(dp.begin(), dp.end());
    int maxIndex = max_element(dp.begin(), dp.end()) - dp.begin();
    vector<int> gasStations;

    while (maxIndex >= 0) {
        gasStations.push_back(maxIndex);
        maxIndex = previous[maxIndex];
    }

    cout << maxProfit << endl;
    cout << gasStations.size() << " ";
    for (int i = gasStations.size() - 1; i >= 0; i--) {
        cout << gasStations[i] + 1 << " ";  // +1 mert 1-től indexelünk
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base12/55
1Wrong answer0/03ms1808 KiB
2Wrong answer0/04ms2024 KiB
3Accepted3/33ms2212 KiB
4Accepted3/32ms2300 KiB
5Wrong answer0/32ms2448 KiB
6Accepted3/32ms2384 KiB
7Wrong answer0/33ms2664 KiB
8Accepted3/32ms2632 KiB
9Wrong answer0/33ms2736 KiB
10Wrong answer0/33ms2948 KiB
11Wrong answer0/33ms3160 KiB
12Wrong answer0/33ms3412 KiB
13Wrong answer0/43ms3656 KiB
14Wrong answer0/44ms3664 KiB
15Wrong answer0/54ms3772 KiB
16Wrong answer0/64ms3908 KiB
17Wrong answer0/64ms3976 KiB