#include<bits/stdc++.h> using namespace std; const int N = 1<<16; int n,T[N+3],X[N+3][2],Y[N+3][2]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for(int i=1;i<=n;i++){ cin>>T[i]; } X[n][0]=0; Y[n][0]=T[n]; X[n][1]=0; Y[n][1]=T[n]; for(int i = n-1;i>=1;i--){ X[i][0] = X[i+1][1]; Y[i][0] = T[i]; if(Y[i+1][1]<=T[i]){ X[i][0]++; Y[i][0] = -2e9; } X[i][1] = X[i+1][0]; Y[i][1] = T[i]; if(Y[i+1][0]>=T[i]){ X[i][1]++; Y[i][1] = 2e9; } } cout<<min(X[1][0],X[1][1]); 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 | #include<bits/stdc++.h> using namespace std; const int N = 1<<16; int n,T[N+3],X[N+3][2],Y[N+3][2]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for(int i=1;i<=n;i++){ cin>>T[i]; } X[n][0]=0; Y[n][0]=T[n]; X[n][1]=0; Y[n][1]=T[n]; for(int i = n-1;i>=1;i--){ X[i][0] = X[i+1][1]; Y[i][0] = T[i]; if(Y[i+1][1]<=T[i]){ X[i][0]++; Y[i][0] = -2e9; } X[i][1] = X[i+1][0]; Y[i][1] = T[i]; if(Y[i+1][0]>=T[i]){ X[i][1]++; Y[i][1] = 2e9; } } cout<<min(X[1][0],X[1][1]); return 0; } |