#include<bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define sz(x) (int)x.size() #define all(x) x.begin(),x.end() #define sor(x) sort(all(x)) #define rev(x) reverse(all(x)) #define rsz(x) resize(x) #define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef long long ll; typedef pair<int,int>pii; typedef long double ldl; typedef pair<ll,ll>pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vii; typedef vector<pll>vll; typedef unsigned long long ul; const int mX_N = 50010; const int INF = 1e5; int dp[mX_N][3];//najpierw ostatnia, potem przedostatnia//0 - nie zmieniamy, 1-zmieniamy na dolny, 2 - zmienimy na gorne int T[mX_N]; int main() { BOOST int n; cin>>n; for(int i = 0 ; i < n ; i++) cin>>T[i]; int ans = INF; dp[0][1]=1; dp[0][2]=1; for(int i = 1 ; i< n ; i++) { if(i&1) { if(T[i]<=T[i-1]) dp[i][0]=dp[i-1][1]; else dp[i][0]=min(dp[i-1][0] ,dp[i-1][1]); dp[i][1]=dp[i-1][1]+1; dp[i][2]=min(min(dp[i-1][0] ,dp[i-1][1] ) ,dp[i-1][2] )+1; } else { if(T[i]>=T[i-1]) dp[i][0]=dp[i-1][2]; else dp[i][0]=min(dp[i-1][0] ,dp[i-1][2]); dp[i][2]=dp[i-1][2]+1; dp[i][1]=min(min(dp[i-1][0] ,dp[i-1][2] ) ,dp[i-1][1] )+1; } } ans = min(min(dp[n-1][0] , dp[n-1][1]) , dp[n-1][2]); for(int i = 1 ; i< n ; i++) { if(!(i&1)) { if(T[i]<=T[i-1]) dp[i][0]=dp[i-1][1]; else dp[i][0]=min(dp[i-1][0] ,dp[i-1][1]); dp[i][1]=dp[i-1][1]+1; dp[i][2]=min(min(dp[i-1][0] ,dp[i-1][1] ) ,dp[i-1][2] )+1; } else { if(T[i]>=T[i-1]) dp[i][0]=dp[i-1][2]; else dp[i][0]=min(dp[i-1][0] ,dp[i-1][2]); dp[i][2]=dp[i-1][2]+1; dp[i][1]=min(min(dp[i-1][0] ,dp[i-1][2] ) ,dp[i-1][1] )+1; } } ans = min(ans,min(min(dp[n-1][0] , dp[n-1][1]) , dp[n-1][2])); cout<<ans<<'\n'; 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 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 82 83 84 85 86 87 | #include<bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define sz(x) (int)x.size() #define all(x) x.begin(),x.end() #define sor(x) sort(all(x)) #define rev(x) reverse(all(x)) #define rsz(x) resize(x) #define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef long long ll; typedef pair<int,int>pii; typedef long double ldl; typedef pair<ll,ll>pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vii; typedef vector<pll>vll; typedef unsigned long long ul; const int mX_N = 50010; const int INF = 1e5; int dp[mX_N][3];//najpierw ostatnia, potem przedostatnia//0 - nie zmieniamy, 1-zmieniamy na dolny, 2 - zmienimy na gorne int T[mX_N]; int main() { BOOST int n; cin>>n; for(int i = 0 ; i < n ; i++) cin>>T[i]; int ans = INF; dp[0][1]=1; dp[0][2]=1; for(int i = 1 ; i< n ; i++) { if(i&1) { if(T[i]<=T[i-1]) dp[i][0]=dp[i-1][1]; else dp[i][0]=min(dp[i-1][0] ,dp[i-1][1]); dp[i][1]=dp[i-1][1]+1; dp[i][2]=min(min(dp[i-1][0] ,dp[i-1][1] ) ,dp[i-1][2] )+1; } else { if(T[i]>=T[i-1]) dp[i][0]=dp[i-1][2]; else dp[i][0]=min(dp[i-1][0] ,dp[i-1][2]); dp[i][2]=dp[i-1][2]+1; dp[i][1]=min(min(dp[i-1][0] ,dp[i-1][2] ) ,dp[i-1][1] )+1; } } ans = min(min(dp[n-1][0] , dp[n-1][1]) , dp[n-1][2]); for(int i = 1 ; i< n ; i++) { if(!(i&1)) { if(T[i]<=T[i-1]) dp[i][0]=dp[i-1][1]; else dp[i][0]=min(dp[i-1][0] ,dp[i-1][1]); dp[i][1]=dp[i-1][1]+1; dp[i][2]=min(min(dp[i-1][0] ,dp[i-1][1] ) ,dp[i-1][2] )+1; } else { if(T[i]>=T[i-1]) dp[i][0]=dp[i-1][2]; else dp[i][0]=min(dp[i-1][0] ,dp[i-1][2]); dp[i][2]=dp[i-1][2]+1; dp[i][1]=min(min(dp[i-1][0] ,dp[i-1][2] ) ,dp[i-1][1] )+1; } } ans = min(ans,min(min(dp[n-1][0] , dp[n-1][1]) , dp[n-1][2])); cout<<ans<<'\n'; return 0; } |