215472026-01-13 13:23:58algoproBenzinkút üzemeltetés (55)cpp17Wrong answer 15/552ms508 KiB
// UUID: 81de2bda-2818-450a-8594-ac76f3fcc759
#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

	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;
			used[i] = true;
			for(int j=i-1;j>rightmost_idx;j--){
				used[j] = false;
			}
		}else{
			dp[i] = h1;
		}
		// cout  << "dp:\n";
		// for (int s:dp){
		// 	cout << s << " " ;
		// }
		// cout << "\n";
	}
	cout << dp[n] << "\n"; 

	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 << " ";
		}
	}
}
SubtaskSumTestVerdictTimeMemory
base15/55
1Accepted0/01ms316 KiB
2Wrong answer0/01ms316 KiB
3Accepted3/31ms508 KiB
4Accepted3/31ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms508 KiB
7Accepted3/31ms316 KiB
8Wrong answer0/31ms316 KiB
9Wrong answer0/31ms316 KiB
10Wrong answer0/31ms316 KiB
11Wrong answer0/31ms316 KiB
12Wrong answer0/31ms316 KiB
13Wrong answer0/41ms316 KiB
14Wrong answer0/41ms332 KiB
15Wrong answer0/51ms316 KiB
16Wrong answer0/62ms316 KiB
17Wrong answer0/61ms316 KiB