UVA - 10313 Pay the Price

来源:互联网 发布:送男友什么礼物 知乎 编辑:程序博客网 时间:2024/05/21 10:18

初看以为是简单的dp,结果发现直接dp的结果有重复,也就是错误的把不同顺序考虑了进去,所以要用三维,第一维是面值为i的钱,并且是完全背包。

需要注意的是由于dp的初始值全为不合理情况-1,故在最后累加的时候要将-1视为0,这样的错误对我来说还是比较隐蔽的,还是要提高编程能力

#include<cstdio>#include<cstring>#include<algorithm>#define MAX 350#define INF 10000000using namespace std;long long dp[MAX][MAX];void getdp(){    memset(dp,-1,sizeof(dp));    dp[0][0]=1;    for(int i=1;i<MAX;i++){        for(int j=1;j<MAX;j++){            for(int t=1;t<MAX;t++){                if(t-i>=0&&dp[j-1][t-i]!=-1){                    if(dp[j][t]==-1)                        dp[j][t]=dp[j-1][t-i];                    else                        dp[j][t]+=dp[j-1][t-i];                }            }        }    }}int check(int a){    if(a>=300){        return 300;    }    return a;}int main(){    char s[20];    int a,b=INF,c=INF;            long long  ans=0;    getdp();    while(gets(s)){        int dit=sscanf(s,"%d %d %d",&a,&b,&c);        a=check(a),b=check(b),c=check(c);        if(dit==2){            c=b,b=0;        }        if(dit==1){            b=0,c=a;        }        ans=0;        for(int i=b;i<=c;i++)            ans+=(dp[i][a]==-1?0:dp[i][a]);        printf("%lld\n",ans);        b=INF,c=INF;    }    return 0;}



0 0