DP algorithm: POJ 3046

  1. Question meaning: There are T kinds of ants, every kind has a specific amount, in total there are A amount of ants. The value of variables S and B are given, ask how many combinations are there to form s, s+1,…,B amount of ants using T kinds of A amount of ants.

  2. There are 3 kind and 5 ants, and the series is {1,1,2,2,3}
    There are 3 ants in each combination and 5 combinations:{1,1,2} {1,1,3} {1,2,2} {1,2,3} {2,2,3}
    There are 4 ants in each combination and 5 combinations: {1,2,2,3} {1,1,2,2} {1,1,2,3}
    There are 5 ants in each combination and 1 combinations: {1,2,2,3} {1,1,2,2} {1,1,2,3}: {1,1,2,2,3}

  3. DP algorithm
    1) DP algorithm is a classification problem, wich when we calculate dp[i][j], we need to classify them first, which how many kind possiblities there is.
    2) DP algorithm is listing all the solutions. DP algorithm not only lists all possible solutions, but also through calculations, see whether their are better solutions compared to the old solutions, which is dynamic optimization.

  4. DP algorithm for this problem: As I introduced, we need do classification for the answer first.
    1) Case one: We need to find j ants from i-1 ants, which the i kind of ants give 0 ants. This is part of find j ants from i kinds, which is dp[i-1][j].
    2) Case two: We already found j-1 ants from *i kinds, and we need to find 1 ant from the *the i kind, which is
    dp[i][j-1].
    3) However, even though we listed all the combinations, we need to eliminate the overlap between these two combinations, or the repeated solutions. We need to consider the case which depleted all ants in *the i kind in dp[i][j-1]. Therefore, we need to eliminate the possibility of
    “find j-1 ants from *previous i kinds(*the i kind is not included) and there are num[i] ants for the i kind”, which is
    dp[i-1][j-1-num[i]].
    4) After we get case one and case two and the possibility which should be eliminated, we can get
    dp[i-1][j]-dp[i][j-1]-do[i-1][j-1-num[i]].

  5. The C++ code
    #include
    #include <stdlib.h>
    #include
    #include
    using namespace std;
    long long dp[1005][100005],num[1005],mod=1000000;
    int main()
    {
    long long t,a,s,b,i,j,x,ans;
    while(scanf("%I64d%I64d%I64d%I64d",&t,&a,&s,&b)!=EOF)
    {
    memset(num,0,sizeof(num));
    for(i=1;i<=a;i++)
    {
    scanf("%I64d",&x);
    num[x]++;
    }
    for(i=0;i<=t;i++)
    dp[i][0]=1;
    for(i=1;i<=t;i++)
    {
    for(j=1;j<=b;j++)
    {
    if(j-1-num[i]>=0)
    dp[i][j]=(dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1-num[i]]+mod)%mod;
    else
    dp[i][j]=(dp[i][j-1]+dp[i-1][j])%mod;
    }
    }
    ans=0;
    for(i=s;i<=b;i++)
    {
    ans=(ans+dp[t][i])%mod;
    }
    printf("%I64d\n",ans);
    }
    system(“pause”);
    return 0;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值