#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; ll aRos[n+1]; ll aMal[n+1]; for (ll i = 0; i < n; ++i) { ll x; cin >> x; aRos[i] = x; aMal[i] = x; } aRos[n] = 0; // 121 aMal[n] = 0; // 212 ll ansRos = 0; ll ansMal = 0; bool change = false; for (ll i = 0; i < n-1; ++i) { if (change) { if (aRos[i] <= aRos[i+1]) { ansRos++; aRos[i+1] = min(aRos[i+2]-1, aRos[i]-1); } if (aMal[i] >= aMal[i+1]) { ansMal++; aMal[i+1] = max(aMal[i+2]+1, aMal[i]+1); } change = false; }else { if (aRos[i] >= aRos[i+1]) { ansRos++; aRos[i+1] = max(aRos[i+2]+1, aRos[i]+1); } if (aMal[i] <= aMal[i+1]) { ansMal++; aMal[i+1] = min(aMal[i+2]-1, aMal[i]-1); } change = true; } } cout << min(ansMal, ansRos) << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; ll aRos[n+1]; ll aMal[n+1]; for (ll i = 0; i < n; ++i) { ll x; cin >> x; aRos[i] = x; aMal[i] = x; } aRos[n] = 0; // 121 aMal[n] = 0; // 212 ll ansRos = 0; ll ansMal = 0; bool change = false; for (ll i = 0; i < n-1; ++i) { if (change) { if (aRos[i] <= aRos[i+1]) { ansRos++; aRos[i+1] = min(aRos[i+2]-1, aRos[i]-1); } if (aMal[i] >= aMal[i+1]) { ansMal++; aMal[i+1] = max(aMal[i+2]+1, aMal[i]+1); } change = false; }else { if (aRos[i] >= aRos[i+1]) { ansRos++; aRos[i+1] = max(aRos[i+2]+1, aRos[i]+1); } if (aMal[i] <= aMal[i+1]) { ansMal++; aMal[i+1] = min(aMal[i+2]-1, aMal[i]-1); } change = true; } } cout << min(ansMal, ansRos) << endl; return 0; } |