#include<cstdio> #include<algorithm> #define S 50007 using namespace std; int T[S]; int T2[S]; int main(void){ int n; scanf("%d",&n); for(int i = 1; i <= n;i++){ scanf("%d",&T[i]); } T2[1] = T[1]; int p = 0; int p2 = 0; for(int i = 2; i <= n;i++){ T2[i] = T[i]; if(i%2){ if(T2[i] <= T2[i-1]){ p++; T2[i] = 1000000000; } }else{ if(T2[i] >= T2[i-1]){ p++; T2[i] = -1000000000; } } } for(int i = 2; i <= n;i++){ T2[i] = T[i]; if(i%2){ if(T2[i] >= T2[i-1]){ p2++; T2[i] = -1000000000; } }else{ if(T2[i] <= T2[i-1]){ p2++; T2[i] = 1000000000; } } } printf("%d",min(p,p2)); 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 | #include<cstdio> #include<algorithm> #define S 50007 using namespace std; int T[S]; int T2[S]; int main(void){ int n; scanf("%d",&n); for(int i = 1; i <= n;i++){ scanf("%d",&T[i]); } T2[1] = T[1]; int p = 0; int p2 = 0; for(int i = 2; i <= n;i++){ T2[i] = T[i]; if(i%2){ if(T2[i] <= T2[i-1]){ p++; T2[i] = 1000000000; } }else{ if(T2[i] >= T2[i-1]){ p++; T2[i] = -1000000000; } } } for(int i = 2; i <= n;i++){ T2[i] = T[i]; if(i%2){ if(T2[i] >= T2[i-1]){ p2++; T2[i] = -1000000000; } }else{ if(T2[i] <= T2[i-1]){ p2++; T2[i] = 1000000000; } } } printf("%d",min(p,p2)); return 0; } |