Submission #146694

#TimeUsernameProblemLanguageResultExecution timeMemory
146694jwvg0425Safety (NOI18_safety)C++17
100 / 100
80 ms7136 KiB
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#define all(x) (x).begin(), (x).end()
#define xx first
#define yy second

using namespace std;

using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

int main()
{
    i64 n, h;
    scanf("%lld %lld", &n, &h);

    vector<i64> s(n);

    for (int i = 0; i < n; i++)
        scanf("%lld", &s[i]);

    priority_queue<i64> lheap;
    priority_queue<i64, vector<i64>, greater<i64>> rheap;
    i64 ans = 0;
    i64 loff = 0, roff = 0;

    lheap.push(s[0]);
    rheap.push(s[0]);

    for (int i = 1; i < n; i++)
    {
        loff -= h;
        roff += h;

        i64 lend = lheap.top() + loff;
        i64 rend = rheap.top() + roff;

        if (s[i] >= lend && s[i] <= rend)
        {
            lheap.push(s[i] - loff);
            rheap.push(s[i] - roff);
        }
        else if (s[i] < lend)
        {
            ans += lend - s[i];
            lheap.pop();
            lheap.push(s[i] - loff);
            lheap.push(s[i] - loff);
            rheap.push(lend - roff);
        }
        else if (s[i] > rend)
        {
            ans += s[i] - rend;
            rheap.pop();
            lheap.push(rend - loff);
            rheap.push(s[i] - roff);
            rheap.push(s[i] - roff);
        }
    }

    printf("%lld\n", ans);

    return 0;
}

Compilation message (stderr)

safety.cpp: In function 'int main()':
safety.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld %lld", &n, &h);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~
safety.cpp:35:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld", &s[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...