#include <bits/stdc++.h> #define qio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define debug(x) cerr<<#x<<" "<<x<<endl #define ll long long #define st first #define nd second using namespace std; int n, tab[2][50005], res, temp[2]; int main() { qio; cin >> n; for (int i = 1; i <= n; i++) { cin >> tab[0][i]; tab[1][i] = tab[0][i]; } for (int i = 1; i < n; i++) { if (i % 2 == 1) { if (tab[0][i] <= tab[0][i + 1]) { tab[0][i + 1] = -1000000000; temp[0]++; } if (tab[1][i] >= tab[1][i + 1]) { tab[1][i + 1] = 1000000000; temp[1]++; } } if (i % 2 == 0) { if (tab[1][i] <= tab[1][i + 1]) { tab[1][i + 1] = -1000000000; temp[1]++; } if (tab[0][i] >= tab[0][i + 1]) { tab[0][i + 1] = 1000000000; temp[0]++; } } } cout << min(temp[0], temp[1]) << endl; }
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 | #include <bits/stdc++.h> #define qio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define debug(x) cerr<<#x<<" "<<x<<endl #define ll long long #define st first #define nd second using namespace std; int n, tab[2][50005], res, temp[2]; int main() { qio; cin >> n; for (int i = 1; i <= n; i++) { cin >> tab[0][i]; tab[1][i] = tab[0][i]; } for (int i = 1; i < n; i++) { if (i % 2 == 1) { if (tab[0][i] <= tab[0][i + 1]) { tab[0][i + 1] = -1000000000; temp[0]++; } if (tab[1][i] >= tab[1][i + 1]) { tab[1][i + 1] = 1000000000; temp[1]++; } } if (i % 2 == 0) { if (tab[1][i] <= tab[1][i + 1]) { tab[1][i + 1] = -1000000000; temp[1]++; } if (tab[0][i] >= tab[0][i + 1]) { tab[0][i + 1] = 1000000000; temp[0]++; } } } cout << min(temp[0], temp[1]) << endl; } |