Http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=3
Dp[i][state] Represents the minimum number of days to rest when the first I is processed, and then the current status is state.
For example, with 1 for rest, 2 for training, and 3 for exercise.
If the current allowed state of the day is only training.
Then dp[i][3] = inf (indicates impossibility)
Rest is possible at any time. From dp[i-1][1--3] state turn around, + 1 can
That derivative is--24.
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<assert.h>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN = -+ -;intdp[maxn][4];voidWork () {intN; scanf ("%d", &N); Memset (DP,0,sizeofDP); for(inti =1; I <= N; ++i) {intx; scanf ("%d", &x); if(x = =0) {dp[i][1] = min (Dp[i-1][1], min (Dp[i-1][2], Dp[i-1][3])) +1; dp[i][2] = dp[i][3] =inf; } Else if(x = =1) {dp[i][2] = Dp[i-1][1]; dp[i][2] = min (dp[i][2], Dp[i-1][3]); dp[i][1] = min (Dp[i-1][1], min (Dp[i-1][2], Dp[i-1][3])) +1; dp[i][3] =inf; } Else if(x = =2) {dp[i][2] =inf; dp[i][3] = min (Dp[i-1][1], Dp[i-1][2]); dp[i][1] = min (Dp[i-1][1], min (Dp[i-1][2], Dp[i-1][3])) +1; } Else if(x = =3) {dp[i][1] = min (Dp[i-1][1], min (Dp[i-1][2], Dp[i-1][3])) +1; dp[i][2] = min (Dp[i-1][1], Dp[i-1][3]); dp[i][3] = min (Dp[i-1][1], Dp[i-1][2]); } } intAns =inf; for(inti =1; I <=3; ++i) {ans=min (ans, dp[n][i]); } cout<< ans * (- -) <<Endl;}intMain () {#ifdef local freopen ("Data.txt","R", stdin);//freopen ("Data.txt", "w", stdout);#endif intT; scanf ("%d", &t); while(t--) work (); return 0;}
View Code
Problem D: Diligent Ripple 2 DP + derivative