Submission #404895

#TimeUsernameProblemLanguageResultExecution timeMemory
404895SeDunionSafety (NOI18_safety)C++17
100 / 100
60 ms5476 KiB
#include<bits/stdc++.h>
#ifndef LOCAL
#define cerr if(false)cerr
#endif // LOCAL
using namespace std;
using ll = long long;
const int N = 1e6 + 66;
 
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int n; ll d;
	cin >> n >> d;
	priority_queue<ll, vector<ll>, greater<ll>>R;
	priority_queue<ll>L;
	ll ans = 0;
	for (int i = 0 ; i < n ; ++ i) {
		ll x;
		cin >> x;
		if (i == 0) {
			L.push(x + d * i);
			R.push(x - d * i);
			continue;
		}
		if (L.top() <= x + d * i && x - d * i <= R.top()) {
			L.push(x + d * i);
			R.push(x - d * i);
		} else if (L.top() > x + d * i) {
			assert(x - d * i <= R.top());
			ans += L.top() - (x + d * i);
			R.push(L.top() - 2 * d * i);
			L.pop();
			L.push(x + d * i);
			L.push(x + d * i);
		} else { // R.top() < x
			assert(x + d * i >= L.top());
			ans += x - d * i - R.top();
			L.push(R.top() + 2 * d * i);
			R.pop();
			R.push(x - d * i);
			R.push(x - d * i);
		}
	}
	cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...