#include<bits/stdc++.h> using namespace std; int main(){ int n,i,i2,result,k; scanf("%d",&n); int arr[n]; int dp[n][3]; //0 - bez zmian, 1 - zmiana for(i=0;i<n;i++) scanf("%d",&arr[i]); result=n; for(i2=0;i2<2;i2++){ dp[0][0]=0; dp[0][1]=dp[0][2]=1; for(i=1;i<n;i++){ k=((i2+i)%2)*2-1; //1 lub -1 dp[i][0]=dp[i-1][1]; if(arr[i-1]*k<arr[i]*k) dp[i][0]=min(dp[i][0],dp[i-1][0]); dp[i][1]=min(dp[i-1][0],dp[i-1][1])+1; } result=min({result,dp[n-1][0],dp[n-1][1]}); } printf("%d\n",result); 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 | #include<bits/stdc++.h> using namespace std; int main(){ int n,i,i2,result,k; scanf("%d",&n); int arr[n]; int dp[n][3]; //0 - bez zmian, 1 - zmiana for(i=0;i<n;i++) scanf("%d",&arr[i]); result=n; for(i2=0;i2<2;i2++){ dp[0][0]=0; dp[0][1]=dp[0][2]=1; for(i=1;i<n;i++){ k=((i2+i)%2)*2-1; //1 lub -1 dp[i][0]=dp[i-1][1]; if(arr[i-1]*k<arr[i]*k) dp[i][0]=min(dp[i][0],dp[i-1][0]); dp[i][1]=min(dp[i-1][0],dp[i-1][1])+1; } result=min({result,dp[n-1][0],dp[n-1][1]}); } printf("%d\n",result); return 0; } |