215632026-01-13 14:36:08algoproBenzinkút üzemeltetés (55)cpp17Elfogadva 55/553ms1596 KiB
// UUID: 6dd5389b-93d7-446d-adff-0859b219826d
#include <bits/stdc++.h>
using namespace std;

int main() {
	int n, k;
	cin >> n >> k;
	vector<int> t(n+1);
	vector<int> h(n+1);
	// vector<bool> used(n+1, false);

	vector<int> dp(n+1, 0);  // dp[i] means the maximum value if regarding only the first i motorway stops
	vector<vector<int>> ans(n+1);
	
	for(int i=1;i<=n;i++){
		cin >> t[i];
		cin >> h[i];
	}
	
	for (int i=1;i<=n;i++){
		// the value we could gain if we don't pick the current stop at index i:
		// - then we have the same value as if we picked only from the stops 1..(i-1)
		int h1 = dp[i-1];

		// the value we could gain if we pick the current stop at index i:
		// - this means we would need to forget about all previous stops that are within k distance
		// and use the current value instead - but we still keep the value gained from the stops
		// prior to (the left of) the lost stops: so an earlier dp value up to the rightmost possible stop
		// that is still far enough from the current one
		int rightmost_idx = i-1;
		while(t[i] - t[rightmost_idx]< k && rightmost_idx > 0){
			rightmost_idx--;
		}
		int h2;

		// if(rightmost_idx == 0){ // no stops found prior to i that is farther away than k
		// 	h2 = h[i];
		// }else{
			h2 = dp[rightmost_idx] + h[i];
		// }

		if(h2 > h1){// then we will pick the current stop
			dp[i] = h2;
			ans[i] = ans[rightmost_idx];
			ans[i].push_back(i);
			
			// used[i] = true;
			// for(int j=i-1;j>rightmost_idx;j--){
			// 	used[j] = false;
			// }

		}else{
			dp[i] = h1;
			ans[i] = ans[i-1];
		}

		// cout  << "dp:\n";
		// for (int s:dp){
		// 	cout << s << " " ;
		// }
		// cout << "\n";

	}
	cout << dp[n] << "\n"; 

	cout << ans[n].size() << " ";
	for(int i : ans[n]) {
        cout << i << " ";
    }


	// int count = 0;
	// for (int j=1;j<=n;j++){
	// 	if(used[j]){
	// 		count++;
	// 	}
	// }

	// cout << count << " ";
	// for (int j=1;j<=n;j++){
	// 	if(used[j]){
	// 		cout << j << " ";
	// 	}
	// }
}

RészfeladatÖsszpontTesztVerdiktIdőMemória
base55/55
1Elfogadva0/01ms508 KiB
2Elfogadva0/02ms1596 KiB
3Elfogadva3/31ms316 KiB
4Elfogadva3/31ms316 KiB
5Elfogadva3/31ms316 KiB
6Elfogadva3/31ms316 KiB
7Elfogadva3/31ms316 KiB
8Elfogadva3/31ms316 KiB
9Elfogadva3/31ms508 KiB
10Elfogadva3/31ms500 KiB
11Elfogadva3/32ms316 KiB
12Elfogadva3/32ms564 KiB
13Elfogadva4/42ms820 KiB
14Elfogadva4/42ms820 KiB
15Elfogadva5/52ms1080 KiB
16Elfogadva6/62ms1524 KiB
17Elfogadva6/63ms1336 KiB