Submission #563937

#TimeUsernameProblemLanguageResultExecution timeMemory
563937Bill_00Safety (NOI18_safety)C++14
100 / 100
50 ms7092 KiB
#include <bits/stdc++.h>
#define N 1000005
typedef long long ll;
using namespace std;

ll n, x, val, h[N], ans;
priority_queue<ll> pq1, pq2;
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> x;
	cin >> h[1];
	pq1.push(h[1]);
	pq2.push(-h[1]);
	for(ll i = 2; i <= n; i++){
		val += x;
		cin >> h[i];
		ll tp1 = pq1.top();
		ll tp2 = -pq2.top();
		if(tp1 - val <= h[i] && h[i] <= tp2 + val){
			pq1.push(h[i] + val);
			pq2.push(-(h[i] - val));
		}
		else{
			if(tp1 - val > h[i]){
				ans += (tp1 - val - h[i]);
				pq1.push(h[i] + val);
				pq1.push(h[i] + val);
				pq2.push(-(tp1 - 2 * val));
				pq1.pop();
			}
			else{
				ans += (h[i] - val - tp2);
				pq2.push(-(h[i] - val));
				pq2.push(-(h[i] - val));
				pq1.push(tp2 + 2 * val);
				pq2.pop();
			}
		}
	}
	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...