Submission #991996

#TimeUsernameProblemLanguageResultExecution timeMemory
991996starchanSafety (NOI18_safety)C++17
100 / 100
58 ms8060 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define in array<int, 2>
#define pb push_back
#define pob pop_back
#define INF (int)1e17
#define MX (int)3e5+5
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL)
signed main()
{
    fast();
    int n, h; cin >> n >> h;
    vector<int> a(n+1);
    for(int i = 1; i <= n; i++)
        cin >> a[i];

    priority_queue<int> L, R; //R stores the negation of elements.

    int B = -a[1];

    L.push(a[1]); R.push(-a[1]); int ltag, rtag; ltag = rtag = 0;
    //if x in set S, then x+stag is real value stored.

    for(int i = 2; i <= n; i++)
    {
        rtag+=h; ltag-=h; B-=(R.size()*h); B-=a[i];

        int zer = L.top()+ltag; int zer2 = rtag-R.top();
        if(a[i] <= zer)
        {
            L.push(a[i]-ltag); L.push(a[i]-ltag);
            R.push(-(L.top()+ltag-rtag)); L.pop();
            //R.push(-(L.top()+ltag-rtag)); L.pop();
        }
        else if(a[i] >= zer2)
        {
            R.push(rtag-a[i]); R.push(rtag-a[i]);
            L.push(rtag-R.top()-ltag); R.pop();
            //L.push(rtag-R.top()-ltag); R.pop();
        }
        else
        {
            R.push(rtag-a[i]);
            L.push(a[i]-ltag);
        }
    }

    vector<int> v;//we shall find all the R change points, exact values.
    while(R.size())
    {
        v.pb(rtag-R.top());
        R.pop();
    }
    reverse(v.begin(), v.end());
    int res = B;
    for(auto x: v)
        res+=x;
    cout << res << "\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...