215622026-01-13 14:15:03algoproBenzinkút üzemeltetés (55)cpp17Hibás válasz 15/552ms540 KiB
// UUID: f14e89a8-ec44-4dc0-bf28-f8665ca9f5cc
#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<long long> 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)
		long long 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--;
		}
		long long 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 << " ";
		}
	}
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base15/55
1Elfogadva0/01ms316 KiB
2Hibás válasz0/02ms316 KiB
3Elfogadva3/31ms316 KiB
4Elfogadva3/31ms316 KiB
5Elfogadva3/31ms384 KiB
6Elfogadva3/31ms316 KiB
7Elfogadva3/31ms540 KiB
8Hibás válasz0/31ms316 KiB
9Hibás válasz0/31ms316 KiB
10Hibás válasz0/31ms316 KiB
11Hibás válasz0/31ms316 KiB
12Hibás válasz0/31ms316 KiB
13Hibás válasz0/41ms316 KiB
14Hibás válasz0/41ms408 KiB
15Hibás válasz0/51ms316 KiB
16Hibás válasz0/61ms316 KiB
17Hibás válasz0/61ms316 KiB