# Problem D: Diligent Ripple 2 DP + derivative

Source: Internet
Author: User

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

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.