Test instructions: Give t seconds time, n song respectively time A[i], give a length of 678 must sing song, ask the most can sing how many songs (as long as the last time remaining one second, you can sing songs of the song finish)
Use Dp[i] to represent the maximum number of songs to spend I time the backpack capacity is the total time given t-1 (leave one second to sing a song)
, the cost of each song is A[i], then the state transfer equation is
Dp[j]=max (dp[j],dp[j-a[i]]+1);
Write their own time, has been WA later looked at the LRJ code, found that the initialization is not correct, change the initialization is right = =
But why initialize it like this? = =
It was later understood that assigning a special value to the initial state was due to the fact that the State was not valid and could not be transferred from this state, for example, if the initial value is assigned to 0, Dp[99]=0, then a song has not been sung, it has taken 99 seconds,
So it should be initialized to 1 ( -2,-3,-4-5,-6,-7-------and other special values that cannot be taken)
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One #definemod=1e9+7; A using namespacestd; - -typedefLong LongLL; the Const intmaxn=100005; - intA[MAXN],DP[MAXN]; - - intMain () { + intN,t,i,j,ncase,kase=0; -Cin>>ncase; + while(ncase--){ A atCin>>n>>T; - for(i=1; i<=n;i++) cin>>A[i]; - - intans=0; - - for(inti =0; I < T; i++) Dp[i] =-1; indp[0] =0; - to for(i=1; i<=n;i++){ + for(j=t-1; j>=0; j--){ - if(J>=a[i]) Dp[j]=max (dp[j],dp[j-a[i]]+1); the *cn1=Max (ans,dp[j]); $ //printf ("ans=%d\n", ans);Panax Notoginseng //printf ("dp[%d]=%d\n", J,dp[j]); - } the } + A for(i=t-1; i>=0; i--){ the if(dp[i]==ans) { +printf"Case %d:%d%d\n", ++kase, ans +1, i +678); - Break; $ } $ } - } - return 0; the}
View Code
UVa 12563 Jin Ge jin Qu hao "01 backpack"