标签:
2 900 250
0 50记忆化搜索代码:/* Problem : 1248 ( 寒冰王座 ) Judge Status : Accepted RunId : 14251353 Language : G++ Author : 2486 Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=1e4+5; int T,n; int dp[maxn]; int S[3]={150,200,350}; int dfs(int s){ if(dp[s]!=-1)return dp[s]; int ans=0; for(int i=0;i<3;i++){ if(s-S[i]<0)continue;//钱不够买东西了 ans=max(dfs(s-S[i])+S[i],ans); } return dp[s]=ans; } int main(){ scanf("%d",&T); memset(dp,-1,sizeof(dp)); while(T--){ scanf("%d",&n); dfs(n); printf("%d\n",n-dp[n]); } return 0; }完全背包代码:/* Problem : 1248 ( 寒冰王座 ) Judge Status : Accepted RunId : 14251499 Language : G++ Author : 2486 Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=1e4+5; int T,n; int dp[maxn]; int S[3]= {150,200,350}; int main() { scanf("%d",&T); memset(dp,0,sizeof(dp)); while(T--) { scanf("%d",&n); for(int i=0;i<3;i++){ for(int j=S[i];j<=n;j++){ dp[j]=max(dp[j],dp[j-S[i]]+S[i]); } } printf("%d\n",n-dp[n]); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/qq_18661257/article/details/47146711