DP分类
CodeForces - 699C - Vacations 动态规划
(找最小值dp)
#include<bits/stdc++.h> using namespace std; int dp[1000][3]; int a[1000]; int main(){ int n ; //求最小值,则赋值为inf memset(dp,100,sizeof(dp)); cin >> n; for(int i = 1 ; i <= n ; i++) cin >> a[i]; dp[0][0] = 0; for(int i = 1; i <= n ; i++) { dp[i][0] = min(min(dp[i-1][1],dp[i-1][2]),dp[i-1][0]) + 1; if(a[i] == 2 || a[i] == 3) dp[i][1] = min(dp[i-1][0],dp[i-1][2]); if(a[i] == 1 || a[i] == 3) dp[i][2] = min(dp[i-1][1] ,dp[i-1][0]); } cout << min(dp[n][0],min(dp[n][1],dp[n][2])) << endl; return 0; }