Submission #347787

#TimeUsernameProblemLanguageResultExecution timeMemory
347787ncduy0303Safety (NOI18_safety)C++17
100 / 100
66 ms5696 KiB
#include <bits/stdc++.h> using namespace std; #define ar array #define ll long long const int MAX_N = 2e5 + 1; const int MOD = 1e9 + 7; const int INF = 1e9; const ll LINF = 1e18; void solve() { int n, m; cin >> n >> m; priority_queue<ll> L; priority_queue<ll,vector<ll>,greater<ll>> R; ll ans = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (i == 0) L.push(x), R.push(x); else { ll shift = (ll)i * m; ll l = L.top() - shift; ll r = R.top() + shift; if (x < l) { L.push(x + shift); L.push(x + shift); L.pop(); R.push(l - shift); ans += l - x; } else if (x > r) { R.push(x - shift); R.push(x - shift); R.pop(); L.push(r + shift); ans += x - r; } else { L.push(x + shift); R.push(x - shift); } } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int tc = 1; // cin >> tc; for (int t = 1; t <= tc; t++) { // cout << "Case #" << t << ": "; 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...