#include <iostream> #include <algorithm> using namespace std; int main() { int N,ansA,ansB,INF; ansA = ansB = 0; INF = 1 << 22; int a[50000],b[50000],c; cin>>N; for(int i=0; i<N; ++i) { cin>>c; a[i] = b[i] = c; } for(int i=1; i<N; ++i) { //pierwszy wiekszy if(i % 2 == 1) { if(a[i] >= a[i-1]) { ansA++; a[i] = -INF; } } if(i % 2 == 0) { if(a[i] <= a[i-1]) { ansA++; a[i] = INF; } } } for(int i=1; i<N; ++i) { //pierwszy mniejszy if(i % 2 == 1) { if(b[i] <= b[i-1]) { ansB++; b[i] = INF; } } if(i % 2 == 0) { if(b[i] >= b[i-1]) { ansB++; b[i] = -INF; } } } int ans = min(ansA,ansB); cout<<ans<<endl; 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 | #include <iostream> #include <algorithm> using namespace std; int main() { int N,ansA,ansB,INF; ansA = ansB = 0; INF = 1 << 22; int a[50000],b[50000],c; cin>>N; for(int i=0; i<N; ++i) { cin>>c; a[i] = b[i] = c; } for(int i=1; i<N; ++i) { //pierwszy wiekszy if(i % 2 == 1) { if(a[i] >= a[i-1]) { ansA++; a[i] = -INF; } } if(i % 2 == 0) { if(a[i] <= a[i-1]) { ansA++; a[i] = INF; } } } for(int i=1; i<N; ++i) { //pierwszy mniejszy if(i % 2 == 1) { if(b[i] <= b[i-1]) { ansB++; b[i] = INF; } } if(i % 2 == 0) { if(b[i] >= b[i-1]) { ansB++; b[i] = -INF; } } } int ans = min(ansA,ansB); cout<<ans<<endl; return 0; } |