#include <bits/stdc++.h> using namespace std; #define ll long long #define loop(i,x,n) for(ll i=x;i<n;i++) #define loopr(i,x,n) for(ll i = n-1;i>=x;i--) #define all(v) v.begin(),v.end() #define print(v) for(auto xx : v){cout<<xx<<" ";}cout<<endl; #define debug if(DEBUG) cout #define ff first #define ss second const ll Mod = 1E9+7, L = 1E18, N = 1E6+9; #ifdef ONLINE_JUDGE bool DEBUG = false; #else bool DEBUG = true; #endif int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> v(n); for(auto &i : v) cin >> i; auto v2 = v; int ans1 = 0, ans2 = 0; for(int i = 1;i<n;i++){ // big small... if (i%2 == 1) { if (v[i] < v[i-1]) continue; else { v[i] = -1E9; ans1++; } } else { if (v[i] > v[i+1]) continue; else { v[i] = 1E9; ans1++; } } } v = v2; for(int i = 1;i<n;i++){ if (i%2 == 1){ if (v[i] > v[i+1]) continue; else { v[i] = 1E9; ans2++; } } else{ if (v[i] < v[i-1]) continue; else { v[i] = -1E9; ans2++; } } } cout << min(ans1, ans2); }
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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define loop(i,x,n) for(ll i=x;i<n;i++) #define loopr(i,x,n) for(ll i = n-1;i>=x;i--) #define all(v) v.begin(),v.end() #define print(v) for(auto xx : v){cout<<xx<<" ";}cout<<endl; #define debug if(DEBUG) cout #define ff first #define ss second const ll Mod = 1E9+7, L = 1E18, N = 1E6+9; #ifdef ONLINE_JUDGE bool DEBUG = false; #else bool DEBUG = true; #endif int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> v(n); for(auto &i : v) cin >> i; auto v2 = v; int ans1 = 0, ans2 = 0; for(int i = 1;i<n;i++){ // big small... if (i%2 == 1) { if (v[i] < v[i-1]) continue; else { v[i] = -1E9; ans1++; } } else { if (v[i] > v[i+1]) continue; else { v[i] = 1E9; ans1++; } } } v = v2; for(int i = 1;i<n;i++){ if (i%2 == 1){ if (v[i] > v[i+1]) continue; else { v[i] = 1E9; ans2++; } } else{ if (v[i] < v[i-1]) continue; else { v[i] = -1E9; ans2++; } } } cout << min(ans1, ans2); } |