#include<bits/stdc++.h> using namespace std; int tab[50000]; int main() { int n, w =0, m = 0, temp = 1, temp2 = 1, i; cin >> n; for(i = 0; i < n; i++) {cin >> tab[i];} for(i = 1; i < n; i++) { if(i % 2 == 1) { if(tab[i-1] >= tab[i]) temp++; else { w += temp/2; temp = 1; } } else { if(tab[i-1] <= tab[i]) temp++; else { w += temp/2; temp = 1; } } if(i % 2 == 1) { if(tab[i-1] <= tab[i]) temp2++; else { m += temp2/2; temp2 = 1; } } else { if(tab[i-1] >= tab[i]) temp2++; else { m += temp2/2; temp2 = 1; } } } m += temp2/2; w += temp/2; cout << min(w,m); 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 | #include<bits/stdc++.h> using namespace std; int tab[50000]; int main() { int n, w =0, m = 0, temp = 1, temp2 = 1, i; cin >> n; for(i = 0; i < n; i++) {cin >> tab[i];} for(i = 1; i < n; i++) { if(i % 2 == 1) { if(tab[i-1] >= tab[i]) temp++; else { w += temp/2; temp = 1; } } else { if(tab[i-1] <= tab[i]) temp++; else { w += temp/2; temp = 1; } } if(i % 2 == 1) { if(tab[i-1] <= tab[i]) temp2++; else { m += temp2/2; temp2 = 1; } } else { if(tab[i-1] >= tab[i]) temp2++; else { m += temp2/2; temp2 = 1; } } } m += temp2/2; w += temp/2; cout << min(w,m); return 0; } |