Submission #482846

#TimeUsernameProblemLanguageResultExecution timeMemory
482846valerikkSafety (NOI18_safety)C++17
100 / 100
50 ms3744 KiB
// https://oj.uz/problem/view/NOI18_safety
#include <stdio.h>
#include <queue>
#include <vector>

int main() {
  int n;
  long long h;
  scanf("%d%lld", &n, &h);
  std::priority_queue<long long> neg;
  long long negd = 0;
  std::priority_queue<long long, std::vector<long long>, std::greater<long long>> pos;
  long long posd = 0;
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    long long a;
    scanf("%lld", &a);
    if (i == 0) {
      neg.push(a);
      pos.push(a);
    } else {
      negd -= h;
      posd += h;
      long long l = neg.top() + negd;
      long long r = pos.top() + posd;
      if (a >= l && a <= r) {
        neg.push(a - negd);
        pos.push(a - posd);
      } else if (a > r) {
        ans += a - r;
        neg.push(r - negd);
        pos.pop();
        pos.push(a - posd);
        pos.push(a - posd);
      } else {
        ans += l - a;
        pos.push(l - posd);
        neg.pop();
        neg.push(a - negd);
        neg.push(a - negd);
      }
    }
  }
  printf("%lld\n", ans);
}

Compilation message (stderr)

safety.cpp: In function 'int main()':
safety.cpp:9:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |   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);
      |     ~~~~~^~~~~~~~~~~~
#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...