#include <bits/stdc++.h> using namespace std; int ceil2(int n) { if(n%2==0) return n/2; return n/2+1; } int main() { int n, a, aprev; scanf("%d", &n); int slope=1; int count1=0; int count2=0; int changes1=0; int changes2=0; scanf("%d",&aprev); for (int i = 1; i < n; i++) { scanf("%d", &a); // printf("%d %d\n",aprev,a); int ok1=false; int ok2=false; if((a-aprev)*slope > 0) ok1=true; if((a-aprev)*slope < 0) ok2=true; if(!ok1) count1++; else { changes1+=ceil2(count1); count1=0; } if(!ok2) count2++; else { changes2+=ceil2(count2); count2=0; } slope = -slope; aprev=a; } changes1+=ceil2(count1); changes2+=ceil2(count2); int changes=min(changes1,changes2); // printf("%d\n",changes1); // printf("%d\n",changes2); printf("%d\n",changes); 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 ceil2(int n) { if(n%2==0) return n/2; return n/2+1; } int main() { int n, a, aprev; scanf("%d", &n); int slope=1; int count1=0; int count2=0; int changes1=0; int changes2=0; scanf("%d",&aprev); for (int i = 1; i < n; i++) { scanf("%d", &a); // printf("%d %d\n",aprev,a); int ok1=false; int ok2=false; if((a-aprev)*slope > 0) ok1=true; if((a-aprev)*slope < 0) ok2=true; if(!ok1) count1++; else { changes1+=ceil2(count1); count1=0; } if(!ok2) count2++; else { changes2+=ceil2(count2); count2=0; } slope = -slope; aprev=a; } changes1+=ceil2(count1); changes2+=ceil2(count2); int changes=min(changes1,changes2); // printf("%d\n",changes1); // printf("%d\n",changes2); printf("%d\n",changes); return 0; } |