1346. Intervals of Monotonicity(dp)
简单dp
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 using namespace std; 8 #define N 200010 9 int p[N],dp[N][2]; 10 int main() 11 { 12 int i,a,b; 13 scanf("%d%d",&a,&b); 14 for(i = 1 ; i <= b-a+1 ; i++) 15 scanf("%d",&p[i]); 16 dp[1][0] = 1; 17 dp[1][1] = 1; 18 for(i = 2; i <= b-a+1 ; i++) 19 { 20 if(p[i]>p[i-1]) 21 { 22 dp[i][0] = min(dp[i-1][1]+1,dp[i-1][0]); 23 dp[i][1] = min(dp[i-1][0]+1,dp[i-1][1]+1); 24 } 25 else if(p[i]<p[i-1]) 26 { 27 dp[i][0] = min(dp[i-1][1]+1,dp[i-1][0]+1); 28 dp[i][1] = min(dp[i-1][0]+1,dp[i-1][1]); 29 } 30 else 31 { 32 dp[i][0] = min(dp[i-1][0],dp[i-1][1]+1); 33 dp[i][1] = min(dp[i-1][1],dp[i-1][0]+1); 34 } 35 } 36 int ans = min(dp[b-a+1][0],dp[b-a+1][1]); 37 printf("%d\n",ans); 38 return 0; 39 }