#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define st first #define nd second #define pb push_back #define mp make_pair #define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() const int mxN = 5e4+7, INF = 1e9; int b[mxN], a[mxN]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i,1,n) cin >> a[i]; rep(i,1,n) b[i] = a[i]; int res0=0,res1=0; rep(i,2,n){ if(i%2==0 && b[i-1] >= b[i]) b[i] = INF, res0++; if(i%2==1 && b[i-1] <= b[i]) b[i] = -INF, res0++; } rep(i,1,n) b[i] = a[i]; rep(i,2,n){ if(i%2==0 && b[i-1] <= b[i]) b[i] = -INF, res1++; if(i%2==1 && b[i-1] >= b[i]) b[i] = INF, res1++; } cout << min(res0,res1) << "\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define st first #define nd second #define pb push_back #define mp make_pair #define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() const int mxN = 5e4+7, INF = 1e9; int b[mxN], a[mxN]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i,1,n) cin >> a[i]; rep(i,1,n) b[i] = a[i]; int res0=0,res1=0; rep(i,2,n){ if(i%2==0 && b[i-1] >= b[i]) b[i] = INF, res0++; if(i%2==1 && b[i-1] <= b[i]) b[i] = -INF, res0++; } rep(i,1,n) b[i] = a[i]; rep(i,2,n){ if(i%2==0 && b[i-1] <= b[i]) b[i] = -INF, res1++; if(i%2==1 && b[i-1] >= b[i]) b[i] = INF, res1++; } cout << min(res0,res1) << "\n"; } |