#include<bits/stdc++.h> using namespace std; const int maxn=50009; int rosnie[maxn], maleje[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, a; cin >> n; for(int i=1; i<=n; i++) { cin >> a; rosnie[i]=a; maleje[i]=a; } int wr=0, wm=0; for(int i=2; i<=n; i++) { if(i%2==1) { if(rosnie[i]<=rosnie[i-1]) { wr++; rosnie[i]=1000000000; } if(maleje[i]>=maleje[i-1]) { wm++; maleje[i]=-1000000000; } } else { if(rosnie[i]>=rosnie[i-1]) { wr++; rosnie[i]=-1000000000; } if(maleje[i]<=maleje[i-1]) { wm++; maleje[i]=1000000000; } } } int wyn=min(wm, wr); cout << wyn; }
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 | #include<bits/stdc++.h> using namespace std; const int maxn=50009; int rosnie[maxn], maleje[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, a; cin >> n; for(int i=1; i<=n; i++) { cin >> a; rosnie[i]=a; maleje[i]=a; } int wr=0, wm=0; for(int i=2; i<=n; i++) { if(i%2==1) { if(rosnie[i]<=rosnie[i-1]) { wr++; rosnie[i]=1000000000; } if(maleje[i]>=maleje[i-1]) { wm++; maleje[i]=-1000000000; } } else { if(rosnie[i]>=rosnie[i-1]) { wr++; rosnie[i]=-1000000000; } if(maleje[i]<=maleje[i-1]) { wm++; maleje[i]=1000000000; } } } int wyn=min(wm, wr); cout << wyn; } |