#include <iostream> #include <vector> #include <algorithm> #define lol long long #define def_times(a, b) int a = 0; a<b; a++ using namespace std; inline const lol tmax = 1000 * 1000 * 1000; inline const lol tmin = -1000 * 1000 * 1000; int main() { int n; cin >> n; vector<lol> twit(n); for(def_times(i, n)) cin >> twit[i]; int sol1 = 0; lol last = twit[0]; if(last != tmax) for(def_times(i, n-1)) // i%2==0 => rosn. { if(i%2==0) { if(last >= twit[i+1]) { last = tmax; sol1++; } else last = twit[i+1]; } else { if(last <= twit[i+1]) { last = tmin; sol1++; } else last = twit[i+1]; } } else sol1 = INT32_MAX; int sol2 = 0; last = twit[0]; if(last != tmin) for(def_times(i, n-1)) // i%2==0 => mal. { if(i%2==0) { if(last <= twit[i+1]) { last = tmin; sol2++; } else last = twit[i+1]; } else { if(last >= twit[i+1]) { last = tmax; sol2++; } else last = twit[i+1]; } } else sol2 = INT32_MAX; cout << (sol1>sol2 ? sol2 : sol1); }
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #include <iostream> #include <vector> #include <algorithm> #define lol long long #define def_times(a, b) int a = 0; a<b; a++ using namespace std; inline const lol tmax = 1000 * 1000 * 1000; inline const lol tmin = -1000 * 1000 * 1000; int main() { int n; cin >> n; vector<lol> twit(n); for(def_times(i, n)) cin >> twit[i]; int sol1 = 0; lol last = twit[0]; if(last != tmax) for(def_times(i, n-1)) // i%2==0 => rosn. { if(i%2==0) { if(last >= twit[i+1]) { last = tmax; sol1++; } else last = twit[i+1]; } else { if(last <= twit[i+1]) { last = tmin; sol1++; } else last = twit[i+1]; } } else sol1 = INT32_MAX; int sol2 = 0; last = twit[0]; if(last != tmin) for(def_times(i, n-1)) // i%2==0 => mal. { if(i%2==0) { if(last <= twit[i+1]) { last = tmin; sol2++; } else last = twit[i+1]; } else { if(last >= twit[i+1]) { last = tmax; sol2++; } else last = twit[i+1]; } } else sol2 = INT32_MAX; cout << (sol1>sol2 ? sol2 : sol1); } |