Submission #1046434

#TimeUsernameProblemLanguageResultExecution timeMemory
1046434Zero_OPSafety (NOI18_safety)C++14
100 / 100
34 ms6312 KiB
#include<bits/stdc++.h>

using namespace std;

#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(v) "[" #v " = " << (v) << "]"
#define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

template<typename T> 
    bool minimize(T& a, const T& b){
        if(a > b){
            return a = b, true;
        }  return false;
    }

template<typename T>
    bool maximize(T& a, const T& b){    
        if(a < b){
            return a = b, true;
        } return false;
    }

template<int dimension, typename T>
struct tensor : public vector<tensor<dimension - 1, T>> {
    static_assert(dimension > 0, "Dimension must be positive !\n");
    template<typename... Args>
    tensor(int n = 0, Args... args) : vector<tensor<dimension - 1, T>> (n, tensor<dimension - 1, T>(args...)) {}
};
 
template<typename T>
struct tensor<1, T> : public vector<T> {
    tensor(int n = 0, T val = T()) : vector<T>(n, val) {}
};

const int MAX = 2e5 + 5;

int n, H, a[MAX];

void testcase(){
    cin >> n >> H;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }    

    priority_queue<long long> lefts;
    priority_queue<long long, vector<long long>, greater<long long>> rights;

    long long ans = 0;
    lefts.push(a[0]); rights.push(a[0]);
    for(int i = 1; i < n; ++i){
        long long shifted = 1LL * i * H;
        long long left_border = lefts.top() - shifted;
        long long right_border = rights.top() + shifted;
        if(a[i] < left_border){
            lefts.push(a[i] + shifted);
            lefts.push(a[i] + shifted);
            lefts.pop();
            rights.push(left_border - shifted);
            ans += abs(left_border - a[i]);
        } else if(a[i] > right_border){
            rights.push(a[i] - shifted);
            rights.push(a[i] - shifted);
            rights.pop();
            lefts.push(right_border + shifted);
            ans += abs(right_border - a[i]);
        } else{
            lefts.push(a[i] + shifted);
            rights.push(a[i] - shifted);
        }
    }
    cout << ans << '\n';
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    file("task");

    int T = 1;
    // cin >> T;
    while(T--){
        testcase();
    }

    return 0;
}

Compilation message (stderr)

safety.cpp: In function 'int main()':
safety.cpp:9:55: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
safety.cpp:80:5: note: in expansion of macro 'file'
   80 |     file("task");
      |     ^~~~
safety.cpp:9:88: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
safety.cpp:80:5: note: in expansion of macro 'file'
   80 |     file("task");
      |     ^~~~
#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...