#include <bits/stdc++.h> using namespace std; int n; int tab[50001]; int check(bool inc) { int prev = tab[0]; int ret = 0; for(int i=1;i<n;i++) { int cur = tab[i]; if(inc) { if(cur <= prev) { cur = 1000000001; ret++; } } else { if(cur >= prev) { cur = -1000000001; ret++; } } prev = cur; inc = !inc; } return ret; } int main() { cin >> n; for(int i=0;i<n;i++) { cin >> tab[i]; } cout << min(check(true), check(false)); 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 | #include <bits/stdc++.h> using namespace std; int n; int tab[50001]; int check(bool inc) { int prev = tab[0]; int ret = 0; for(int i=1;i<n;i++) { int cur = tab[i]; if(inc) { if(cur <= prev) { cur = 1000000001; ret++; } } else { if(cur >= prev) { cur = -1000000001; ret++; } } prev = cur; inc = !inc; } return ret; } int main() { cin >> n; for(int i=0;i<n;i++) { cin >> tab[i]; } cout << min(check(true), check(false)); return 0; } |