This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
using namespace std;
template <typename T>
using matrix = vector<vector<T>>;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INFL = (1LL<<62)-1;
const int INF = (1<<30)-1;
const double EPS = 1e-7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6+1;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
ll h;
vector<ll> v(n);
cin >> n >> h;
for(int i = 0; i < n; i++)
cin >> v[i];
priority_queue<ll> left;
priority_queue<ll,vector<ll>,greater<ll>> right;
left.push(v[0]);
right.push(v[0]);
ll offset = 0;
ll resp = 0;
for(int i = 1; i < n; i++){
offset+=h;
pll last = {left.top()-offset,right.top()+offset};
if(v[i]<= last.ff){
//cout << 'a' << '\n';
left.push(v[i]+offset);
left.push(v[i]+offset);
right.push(left.top()-offset*2);
if(left.empty())
exit(1);
left.pop();
resp+=max(0ll,last.ff-v[i]);
} else {
right.push(v[i]-offset);
right.push(v[i]-offset);
left.push(right.top() +2*offset);
if(right.empty())
exit(1);
right.pop();
resp+=max(0ll,v[i]-last.ss);
}
/*cout << "last = " << last.ff << ' ' << last.ss << '\n';
cout << "left: " ;
for(int j : left) cout << j -offset<< ' ';
cout << "\nright: ";
for(int j : right) cout << j +offset<< ' ';
cout << "\nresp(" << i <<") = " << resp << '\n';
cout << '\n';*/
}
cout << resp << '\n';
return 0;
}
# | 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... |