Analysis
First, assume the first hour of the day is not connected to the N-th day after hours, DP transfer miss compare this case
dp[i][j][0/1]dp[i][j][0/1]表示
Consider before i hours a day, have a rest j hours, and the i-th hour whether at rest
Then there is the state transition equation:
dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][1]);
dp[i][j][1]=max(dp[i-1][j-1][0],dp[i-1][j-1][1]+a[i]);
Is initialized to DP [. 1] [0] [0] = DP [. 1] [. 1] [. 1] = 0 D P [ . 1 ] [ 0 ] [ 0 ] = D P [ . 1 ] [ . 1 ] [ . 1 ] = 0 , the remainder being -INF - I n- F
Answer is max (DP [n-] [B] [0], DP [n-] [B] [. 1]) m A X ( D P [ n- ] [ B ] [ 0 ] , D P [ n- ] [ B ] [ 1 ] )
Connected to consider the N-th day and hour of the day after the first hour now
We found that the above transfer, the only thing taken into account only if the first one hour of rest to get physical
So we can initialize DP [. 1] [. 1] [. 1] = U_1 D P [ . 1 ] [ . 1 ] [ . 1 ] = the U- . 1 , the same as above transfer equation
Then the answer to dp [n-] [B] [. 1] D P [ n- ] [ B ] [ . 1 ] (i.e., the last hour of rest in order to force the first hour to obtain a strength), and comparing the previous answer can be obtained dp Final Results
So far here already AC, but! ! ! If we get the submission POJ, you will find yourself crazy MLE (POJ frenzied Memory limit only 64M)
So we consider the use of an array of space-optimized rolling
dp[i&1][j][0]=max(dp[(i-1)&1][j][0],dp[(i-1)&1][j][1]);
dp[i&1][j][1]=max(dp[(i-1)&1][j-1][0],dp[(i-1)&1][j-1][1]+a[i]);
Because dp [I] [] [] with only dp [i-1] [] [] related, so long as the alternating array of dimension 0 and dimension 1, save only the last update dp array can greatly optimize the space
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define int long long 6 #define maxn 3830+10 7 using namespace std; 8 inline int read() 9 { 10 int x=0; 11 bool f=1; 12 char c=getchar(); 13 for(; !isdigit(c); c=getchar()) if(c=='-') f=0; 14 for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0'; 15 if(f) return x; 16 return 0-x; 17 } 18 inline void write(int x) 19 { 20 if(x<0){putchar('-');x=-x;} 21 if(x>9)write(x/10); 22 putchar(x%10+'0'); 23 } 24 int n,b,ans; 25 int u[maxn]; 26 int dp1[2][maxn][2],dp2[2][maxn][2]; 27 signed main() 28 { 29 // freopen("naptime.in","r",stdin); 30 // freopen("naptime.out","w",stdout); 31 n=read();b=read(); 32 for(int i=1;i<=n;i++) u[i]=read(); 33 memset(dp1,128,sizeof(dp1)); 34 dp1[1][0][0]=dp1[1][1][1]=0; 35 memset(dp2,128,sizeof(dp2)); 36 dp2[1][1][1]=u[1]; 37 for(int i=2;i<=n;i++) 38 { 39 for(int j=0;j<=min(i,b);j++) 40 { 41 dp1[i&1][j][0]=max(dp1[(i-1)&1][j][0],dp1[(i-1)&1][j][1]); 42 if(j>=1) dp1[i&1][j][1]=max(dp1[(i-1)&1][j-1][0],dp1[(i-1)&1][j-1][1]+u[i]); 43 dp2[i&1][j][0]=max(dp2[(i-1)&1][j][0],dp2[(i-1)&1][j][1]); 44 if(j>=1) dp2[i&1][j][1]=max(dp2[(i-1)&1][j-1][0],dp2[(i-1)&1][j-1][1]+u[i]); 45 } 46 } 47 ans=max(dp2[n&1][b][1],max(dp1[n&1][b][0],dp1[n&1][b][1])); 48 write(ans); 49 return 0; 50 }
Please Gangster treatise(Anyway, I do not know what that means treatise)