#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define SZ(x) ((int) (x).size())
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define debug(x) cout << "[" << #x << " = " << (x) << "]" << endl
#define left __left
#define right __right
#define prev __prev
#define next __next
#define fi first
#define se second
template <class X, class Y>
bool maximize(X &x, Y y) {
if (x < y) return x = y, true;
else return false;
}
template <class X, class Y>
bool minimize(X &x, Y y) {
if (x > y) return x = y, true;
else return false;
}
#define MAX_N 200'200
int a[MAX_N + 2], b[MAX_N + 2];
ll prefix[MAX_N + 2], suffix[MAX_N + 2];
int n;
int main() {
ios_base::sync_with_stdio(false);cin.tie(nullptr);
#define task "test"
if (fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n;
FOR(i, 1, n) cin >> a[i];
FOR(i, 1, n - 1) b[i] = a[i + 1] - a[i];
// peak = K;
/**
1.. K - 1 (b[i] >= 1)
K ... n - 1 (b[i] <= -1);
**/
/// update[L..R]: b[i];
/// b[L] += 1;
/// b[R] -= 1;
// FOR(i, 0, cout << x << " ";
prefix[0] = 0;
FOR(i, 1, n - 1) {
prefix[i] = prefix[i - 1];
if (b[i] < 1) prefix[i] += 1 - b[i];
}
suffix[n] = 0;
FORD(i, n - 1, 1) {
suffix[i] = suffix[i + 1];
if (b[i] > -1) suffix[i] += b[i] - (-1);
}
ll ans = 1e18;
FOR(peak, 1, n) {
// ll prefix = 0;
// FOR(i, 1, peak - 1) if (b[i] < 1) prefix += 1 - b[i];
//
// ll suffix = 0;
// FOR(i, peak, n - 1) if (b[i] > -1) suffix += b[i] - (-1);
minimize(ans, max(prefix[peak - 1], suffix[peak]));
}
cout << ans;
return 0;
}
/* Discipline - Calm */
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:43:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |