제출 #1334771

#제출 시각아이디문제언어결과실행 시간메모리
1334771NurislamSafety (NOI18_safety)C++20
100 / 100
39 ms5096 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long

const int inf = 1e17, mod = 998244353, N = 5e6+5;


mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rnd(l, r) uniform_int_distribution<int>(l, r)(rng)

int bp (int a, int n, int md) {
	int res = 1, p = a;
	while(n > 0) {
		if(n & 1) res = res * p % md;
		p = p * p % md;
		n >>= 1;
	};
	return res;
};


int gcd(int a, int b, int & x1, int & y1) {
	if(b == 0) {
		x1 = 1;
		y1 = 0;
		return a;
	};
	int x = 0, y = 0;
	int g = gcd(b, a%b, y, x);
	// b * y + a % b * x = g
	// b * y + a * x - b * (a/b)*x = g
	// a * x + b * (y - (a/b)*x) = g
	x1 = x;
	y1 = y - (a/b) * x;
	return g;
};

void solve() {
	int n, h;
	cin >> n >> h;
	vector<int> a(n);
	for(int &i : a) cin >> i;
	
	int ans = 0, vl = 0, vr = 0;
	
	priority_queue<int> l;
	priority_queue<int, vector<int>, greater<int>> r;
	l.push(a[0]);
	r.push(a[0]);
	
	for(int i = 1; i < n; i ++ ) {
		vl -= h;
		vr += h;
		
		if(l.top() + vl <= a[i] && a[i] <= r.top() + vr) {
			l.push(a[i] - vl);
			r.push(a[i] - vr);
			continue;
		};
		if(a[i] < l.top() + vl) {
			int ls = l.top();
			l.pop();
			ans += ls - (a[i] - vl);
			
			l.push(a[i] - vl);
			l.push(a[i] - vl);
			r.push(ls + vl - vr);
			continue;
		};
		if(a[i] > r.top() + vr) { 
			int ls = r.top();
			r.pop();
			
			ans += (a[i] - vr) - ls;
			
			r.push(a[i] - vr);
			r.push(a[i] - vr);
			l.push(ls + vr - vl);
			continue;
		};
	};
	cout << ans << '\n';
};


signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int tt = 1;
	//cin >> tt; 
	
	while( tt -- ) {
		solve();
	};
}; 
















#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...