#include<bits/stdc++.h> using namespace std; int main() { int n, res1 = 0, res2 = 0, res3 = 0, res4 = 0; cin >> n; vector <int> a (n); for(int i = 0; i<n; ++i) { cin >> a[i]; } for(int i = 1; i < n; i += 2) { if((i+1 < n) and (a[i] >= a[i-1] or a[i] >= a[i+1])) res1++; if((i+1 < n) and (a[i] <= a[i-1] or a[i] <= a[i+1])) res2++; if(i+1 == n) { if(a[i] >= a[i-1]) res1++; if(a[i] <= a[i-1]) res2++; } } if(a[0] <= a[1]) res4++; if(a[0] >= a[1]) res3++; if(a[n-1] <= a[n-2]) res4++; if(a[n-1] >= a[n-2]) res3++; for(int i = 2; i < n; i+=2) { if((i+1 < n) and (a[i] >= a[i-1] or a[i] >= a[i+1])) res3++; if((i+1 < n) and (a[i] <= a[i-1] or a[i] <= a[i+1])) res4++; } if(res1 <= res2 and res1 <= res3 and res1 <= res4) cout << res1;//<< " 1"; else if(res2 <= res1 and res2 <= res3 and res2 <= res4) cout << res2;//<< " 2 "; else if(res3 <= res1 and res3 <= res2 and res3 <= res4) cout << res3;//<< " 3 "; else cout << res4;//<< " 4 "; 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; int main() { int n, res1 = 0, res2 = 0, res3 = 0, res4 = 0; cin >> n; vector <int> a (n); for(int i = 0; i<n; ++i) { cin >> a[i]; } for(int i = 1; i < n; i += 2) { if((i+1 < n) and (a[i] >= a[i-1] or a[i] >= a[i+1])) res1++; if((i+1 < n) and (a[i] <= a[i-1] or a[i] <= a[i+1])) res2++; if(i+1 == n) { if(a[i] >= a[i-1]) res1++; if(a[i] <= a[i-1]) res2++; } } if(a[0] <= a[1]) res4++; if(a[0] >= a[1]) res3++; if(a[n-1] <= a[n-2]) res4++; if(a[n-1] >= a[n-2]) res3++; for(int i = 2; i < n; i+=2) { if((i+1 < n) and (a[i] >= a[i-1] or a[i] >= a[i+1])) res3++; if((i+1 < n) and (a[i] <= a[i-1] or a[i] <= a[i+1])) res4++; } if(res1 <= res2 and res1 <= res3 and res1 <= res4) cout << res1;//<< " 1"; else if(res2 <= res1 and res2 <= res3 and res2 <= res4) cout << res2;//<< " 2 "; else if(res3 <= res1 and res3 <= res2 and res3 <= res4) cout << res3;//<< " 3 "; else cout << res4;//<< " 4 "; return 0; } |