Submission #328790

#TimeUsernameProblemLanguageResultExecution timeMemory
328790NirjhorSafety (NOI18_safety)C++17
100 / 100
70 ms7064 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 200010;

ll steady, shift;
int n; ll a[N], h;
priority_queue <ll> L;
priority_queue <ll, vector <ll>, greater <ll>> R;

int main() {
  scanf("%d %lld", &n, &h);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", a + i);
  }
  L.emplace(a[1]), R.emplace(a[1]);
  for (int i = 2; i <= n; ++i) {
    shift += h;
    ll lt = L.top() - shift, rt = R.top() + shift;
    if (a[i] >= lt and a[i] <= rt) {
      L.emplace(a[i] + shift);
      R.emplace(a[i] - shift);
    } else {
      if (a[i] < lt) {
        L.emplace(a[i] + shift);
        L.emplace(a[i] + shift);
        R.emplace(L.top() - shift - shift); L.pop();
        steady += lt - a[i];
      } else {
        R.emplace(a[i] - shift);
        R.emplace(a[i] - shift);
        L.emplace(R.top() + shift + shift); R.pop();
        steady += a[i] - rt;
      }
    }
  }
  cout << steady << '\n';
  return 0;
}

Compilation message (stderr)

safety.cpp: In function 'int main()':
safety.cpp:15:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   15 |   scanf("%d %lld", &n, &h);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~
safety.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   17 |     scanf("%lld", a + i);
      |     ~~~~~^~~~~~~~~~~~~~~
#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...