Submission #508398

#TimeUsernameProblemLanguageResultExecution timeMemory
508398alextodoranSafety (NOI18_safety)C++17
18 / 100
217 ms21328 KiB
/** ____ ____ ____ ____ ____ ||a |||t |||o |||d |||o || ||__|||__|||__|||__|||__|| |/__\|/__\|/__\|/__\|/__\| **/ #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N_MAX = 200000; int N, H; int S[N_MAX + 2]; ll valueAtZero; int deletedLeft; multiset <ll> leftPivots; multiset <ll> rightPivots; ll shiftLeft, shiftRight; void addPivot (int piv) { if (rightPivots.empty() == true || *rightPivots.begin() + shiftRight <= piv) { rightPivots.insert(piv - shiftRight); } else { leftPivots.insert(piv - shiftLeft); } if ((int) leftPivots.size() + deletedLeft > (int) rightPivots.size()) { rightPivots.insert(*prev(leftPivots.end()) + shiftLeft - shiftRight); leftPivots.erase(prev(leftPivots.end())); } if ((int) rightPivots.size() > (int) leftPivots.size() + deletedLeft + 1) { leftPivots.insert(*rightPivots.begin() + shiftRight - shiftLeft); rightPivots.erase(rightPivots.begin()); } } void expand () { shiftLeft -= H; shiftRight += H; while (leftPivots.empty() == false && *leftPivots.begin() < 0) { leftPivots.erase(leftPivots.begin()); deletedLeft++; } valueAtZero -= H * (int) leftPivots.size(); } ll getMin () { ll value = valueAtZero; for (ll piv : leftPivots) { value -= (piv + shiftLeft); } return value; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N >> H; for (int i = 1; i <= N; i++) { cin >> S[i]; } valueAtZero += S[1]; addPivot(S[1]); addPivot(S[1]); for (int i = 2; i <= N; i++) { valueAtZero += S[i]; expand(); addPivot(S[i]); addPivot(S[i]); } cout << getMin() << "\n"; return 0; }
#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...