Submission #245790

#TimeUsernameProblemLanguageResultExecution timeMemory
245790NightlightSafety (NOI18_safety)C++14
100 / 100
68 ms5636 KiB
#include <bits/stdc++.h> using namespace std; int N; long long A[100005]; long long ans; priority_queue<long long, vector<long long>> L; priority_queue<long long, vector<long long>, greater<long long>> R; long long H, now, SL, SR; int main() { ios_base::sync_with_stdio(0); cin >> N >> H; cin >> now; L.push(now); R.push(now); for(int i = 2; i <= N; i++) { cin >> now; SL += H;//shift kiri dan kanan by H SR -= H; long long Lnow = L.top() - SL; long long Rnow = R.top() - SR; // cout << Lnow << " " << Rnow << "\n"; if(now < Lnow) { L.pop();//buang dari kiri masukin ke kanan R.push(Lnow + SR); L.push(now + SL); L.push(now + SL); ans += abs(now - Lnow); }else if(now > Rnow) { R.pop(); L.push(Rnow + SL); R.push(now + SR); R.push(now + SR); ans += abs(now - Rnow); }else { L.push(now + SL); R.push(now + SR); } } cout << ans << "\n"; cin >> N; }
#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...