HDU2697+DP
dp[i][j]:从前i个中挑出某些且cost不超过j的最大val。
dp[i][j]:应该有1到i-1的dp[k][j-?]来更新!!
1 /* 2 DP 3 dp[i][j]:从前i个中挑出某些且cost不超过j的最大val。 4 */ 5 #include<stdio.h> 6 #include<string.h> 7 #include<stdlib.h> 8 #include<algorithm> 9 #include<iostream> 10 #include<queue> 11 #include<map> 12 #include<stack> 13 #include<set> 14 #include<math.h> 15 using namespace std; 16 typedef long long int64; 17 //typedef __int64 int64; 18 typedef pair<int64,int64> PII; 19 #define MP(a,b) make_pair((a),(b)) 20 const int maxn = 105; 21 const int inf = 0x7fffffff; 22 const double pi=acos(-1.0); 23 const double eps = 1e-8; 24 int dp[ maxn ][ maxn ]; 25 int a[ maxn ]; 26 int main(){ 27 int T; 28 scanf("%d",&T); 29 while( T-- ){ 30 int n,sum; 31 scanf("%d%d",&n,&sum); 32 int MinCost = inf; 33 for( int i=1;i<=n;i++ ){ 34 scanf("%d",&a[i]); 35 if( a[i]<MinCost ) MinCost = a[i]; 36 } 37 if( MinCost>sum ){ 38 printf("0\n"); 39 continue; 40 } 41 if( MinCost==sum ){ 42 puts("1"); 43 continue; 44 } 45 int ans = 0; 46 memset( dp,0,sizeof( dp ) ); 47 for( int i=1;i<=n;i++ ){ 48 for( int j=0;j<=sum;j++ ){ 49 int cnt = 0; 50 for( int k=1;k<=i;k++ ){ 51 cnt += a[ i+1-k ]; 52 if( cnt>j ) break; 53 dp[i][j] = max( dp[i][j],dp[i-k][j-cnt]+k*k ); 54 } 55 dp[i][j] = max( dp[i][j],dp[i-1][j] ); 56 ans = max( ans,dp[i][j] ); 57 } 58 } 59 printf("%d\n",ans); 60 } 61 return 0; 62 }
keep moving...