# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1124374 | tuannm | Safety (NOI18_safety) | C++20 | 44 ms | 4452 KiB |
#include<bits/stdc++.h>
#define ii pair<int, int>
#define ll pair<long long, long long>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod[2] = {1000000007, 998244353};
const int N = 2e5 + 1;
const string NAME = "";
int n, h, s[N];
void inp(){
cin >> n >> h;
for(int i = 1; i <= n; ++i)
cin >> s[i];
}
void solve(){
priority_queue<long long> pqL, pqR;
long long ans = 0;
pqL.push(s[1]);
pqR.push(-s[1]);
for(int i = 2; i <= n; ++i){
long long cost = 1LL * h * (i - 1);
long long leftBound = pqL.top() - cost;
long long rightBound = cost - pqR.top();
if(s[i] < leftBound){
pqL.push(cost + s[i]);
pqL.push(cost + s[i]);
pqL.pop();
pqR.push(cost - leftBound);
ans += abs(leftBound - s[i]);
}
else if(s[i] > rightBound){
pqR.push(cost - s[i]);
pqR.push(cost - s[i]);
pqR.pop();
pqL.push(cost + rightBound);
ans += abs(rightBound - s[i]);
}
else{
pqL.push(cost + s[i]);
pqR.push(cost - s[i]);
}
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen((NAME + ".inp").c_str(), "r")){
freopen((NAME + ".inp").c_str(), "r", stdin);
freopen((NAME + ".out").c_str(), "w", stdout);
}
inp();
solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |