#include <bits/stdc++.h> using namespace std; int n; const int N = 5e4 + 3; int ciag1[N], ciag2[N]; int kier1 = 1, kier2 = -1, wyn1, wyn2; int zamien_kierunek(int x) { if (x == 1) return -1; if (x == -1) return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; i++) { cin >> ciag1[i]; ciag2[i] = ciag1[i]; } for (int i = 1; i <= n - 1; i++) { if (ciag1[i + 1] <= ciag1[i] && kier1 == 1) { if (i != n - 1 || i != n - 2) ciag1[i + 1] = max(ciag1[i + 2], ciag1[i]) + 1; else ciag1[i + 1] = ciag1[i] + 1; wyn1++; } if (ciag1[i + 1] >= ciag1[i] && kier1 == -1) { if (i != n - 1 || i != n - 2) ciag1[i + 1] = min(ciag1[i + 2], ciag1[i]) - 1; else ciag1[i + 1] = ciag1[i] - 1; wyn1++; } kier1 = zamien_kierunek(kier1); if (ciag2[i + 1] <= ciag2[i] && kier2 == 1) { if (i != n - 1 || i != n - 2) ciag2[i + 1] = max(ciag2[i + 2], ciag2[i]) + 1; else ciag2[i + 1] = ciag2[i] + 1; wyn2++; } if (ciag2[i + 1] >= ciag2[i] && kier2 == -1) { if (i != n - 1 || i != n - 2) ciag2[i + 1] = min(ciag2[i + 2], ciag2[i]) - 1; else ciag2[i + 1] = ciag2[i] - 1; wyn2++; } kier2 = zamien_kierunek(kier2); } cout << min(wyn1, wyn2); 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 53 54 55 56 57 58 | #include <bits/stdc++.h> using namespace std; int n; const int N = 5e4 + 3; int ciag1[N], ciag2[N]; int kier1 = 1, kier2 = -1, wyn1, wyn2; int zamien_kierunek(int x) { if (x == 1) return -1; if (x == -1) return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; i++) { cin >> ciag1[i]; ciag2[i] = ciag1[i]; } for (int i = 1; i <= n - 1; i++) { if (ciag1[i + 1] <= ciag1[i] && kier1 == 1) { if (i != n - 1 || i != n - 2) ciag1[i + 1] = max(ciag1[i + 2], ciag1[i]) + 1; else ciag1[i + 1] = ciag1[i] + 1; wyn1++; } if (ciag1[i + 1] >= ciag1[i] && kier1 == -1) { if (i != n - 1 || i != n - 2) ciag1[i + 1] = min(ciag1[i + 2], ciag1[i]) - 1; else ciag1[i + 1] = ciag1[i] - 1; wyn1++; } kier1 = zamien_kierunek(kier1); if (ciag2[i + 1] <= ciag2[i] && kier2 == 1) { if (i != n - 1 || i != n - 2) ciag2[i + 1] = max(ciag2[i + 2], ciag2[i]) + 1; else ciag2[i + 1] = ciag2[i] + 1; wyn2++; } if (ciag2[i + 1] >= ciag2[i] && kier2 == -1) { if (i != n - 1 || i != n - 2) ciag2[i + 1] = min(ciag2[i + 2], ciag2[i]) - 1; else ciag2[i + 1] = ciag2[i] - 1; wyn2++; } kier2 = zamien_kierunek(kier2); } cout << min(wyn1, wyn2); return 0; } |