#include<bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define sz(X) ((int)(X).size()) #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; int n; int solve(vi tab) { int x = 2e9,res=0; rep(i,n) { if((i&1) && tab[i] <= x) x = 2e9, res++; else if(!(i&1) && tab[i] >= x) x = -2e9, res++; else x=tab[i]; } return res; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n; vi tab(n); rep(i,n) cin >> tab[i]; int res = solve(tab); for(auto &a:tab) a *= -1; res = min(res, solve(tab)); cout<<res<<"\n"; }
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 | #include<bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define sz(X) ((int)(X).size()) #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; int n; int solve(vi tab) { int x = 2e9,res=0; rep(i,n) { if((i&1) && tab[i] <= x) x = 2e9, res++; else if(!(i&1) && tab[i] >= x) x = -2e9, res++; else x=tab[i]; } return res; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n; vi tab(n); rep(i,n) cin >> tab[i]; int res = solve(tab); for(auto &a:tab) a *= -1; res = min(res, solve(tab)); cout<<res<<"\n"; } |