# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
137824 | SirCeness | Safety (NOI18_safety) | C++14 | 167 ms | 4328 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 mod 1000000007
#define mp make_pair
#define pb push_back
#define bas(x) #x << ": " << x << " "
#define prarr(x, n) cout << #x << ": "; for (int qsd = 0; qsd < n; qsd++) cout << x[qsd] << " "; cout << endl;
#define prarrv(x) cout << #x << ": "; for (int qsd = 0; qsd < (int)x.size(); qsd++) cout << x[qsd] << " "; cout << endl;
#define ppair(x) "(" << x.first << ", " << x.second << ")"
#define inside sl<=l&&r<=sr
#define outside sr<l||r<sl
typedef long long ll;
int main(){
//freopen("in", "r", stdin);
//freopen("out", "w", stdout);
ll n, h;
cin >> n >> h;
vector<ll> arr(n);
for (int i = 0; i < n; i++){
cin >> arr[i];
}
vector<ll> a;
a.pb(arr[0]);
ll ekle = 0;
for (int i = 1; i < n; i++){
if (abs(arr[i] - arr[i-1]) > h){
a.pb(arr[i] + ekle);
} else {
ekle += arr[i-1] - arr[i];
a.pb(arr[i] + ekle);
}
}
//prarrv(a);
vector<int> fark(a.size());
fark[0] = 0;
for (int i = 1; i < a.size(); i++){
if (a[i] > a[i-1]) fark[i] = fark[i-1]+h;
else if (a[i] < a[i-1]) fark[i] = fark[i-1]-h;
else {
fark[i] = fark[i-1];
}
}
//prarrv(fark);
for (int i = 0; i < n; i++) a[i] -= fark[i];
sort(a.begin(), a.end());
ll med = a[a.size()/2];
ll ans = 0;
for (int i = 0; i < a.size(); i++) ans += abs(a[i] - med);
cout << ans << endl;
}
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... |