本文共 1696 字,大约阅读时间需要 5 分钟。
dp[i][j]表示前i种硬币中取总价值为j时第i种硬币最多剩下多少个,-1表示无法到达该状态。
a.当dp[i-1][j]>=0时,dp[i][j]=ci;
b.当j-ai>=0&&dp[i-1][j-ai]>0时,dp[i][j]=dp[i-1][j-ai]-1;
c.其他,dp[i][j]=-1
Source CodeProblem: 1742 User: BManMemory: 1112K Time: 1547MSLanguage: G++ Result: AcceptedSource Code//#pragma comment(linker, "/STACK:1024000000,1024000000")#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair pii;#define pb(a) push(a)#define INF 0x1f1f1f1f#define lson idx<<1,l,mid#define rson idx<<1|1,mid+1,r#define PI 3.1415926535898template T min(const T& a,const T& b,const T& c) { return min(min(a,b),min(a,c));}template T max(const T& a,const T& b,const T& c) { return max(max(a,b),max(a,c));}void debug() {#ifdef ONLINE_JUDGE#else freopen("d:\\in1.txt","r",stdin); freopen("d:\\out1.txt","w",stdout);#endif}int getch() { int ch; while((ch=getchar())!=EOF) { if(ch!=' '&&ch!='\n')return ch; } return EOF;}const int maxn=105;const int maxm=100005;int c[maxn],a[maxn];int dp[maxm];int main(){ //freopen("data.in","r",stdin); int n,m; while(cin>>n>>m) { if(n&&m);else break; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++) cin>>c[i]; memset(dp,-1,sizeof(dp)); dp[0]=0; for(int i=1;i<=n;i++) { for(int j=0;j<=m;j++) { if(dp[j]>=0) dp[j]=c[i]; else if(j>=a[i]&&dp[j-a[i]]>0) dp[j]=dp[j-a[i]]-1; } } int cnt=0; for(int i=1;i<=m;i++) if(dp[i]>=0) cnt++; cout< <
转载于:https://www.cnblogs.com/BMan/p/3680683.html