# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1046434 | Zero_OP | Safety (NOI18_safety) | C++14 | 34 ms | 6312 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |