HDU ACM Steps 3.3.3 Ahui Writes Word || HDU 3732 Ahui Writes Word

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 15000
//典型的多重背包问题,用二进制方法进行物品数量的压缩
int map[15][15];
//背包问题多数用一维就够了,除非有多个费用
int dp[MAXN];

int main()
{
    int n,c,i,j,x,y,k,t,sum;
    char s[100];
    while(scanf("%d%d",&n,&c)!=EOF)
    {
        memset(map,0,sizeof(map));
        memset(dp,0,sizeof(dp));
        //输入数据
        for(i=1; i<=n; i++)
        {
            scanf("%s%d%d",s,&x,&y);//x--v   y--c
            map[x][y]++;//记录价值和复杂度都相同的单词个数
        }

        for(i=0; i<=10; i++)//Each of the next N line are a string and two integer, representing the word,
            for(j=0; j<=10; j++)//the value(Vi ) and the complexity(Ci ). (0 ≤ Vi , Ci ≤ 10)
            {
                if(map[i][j]==0)
                    continue;
                if(map[i][j]*j>=c)
                {
                    for(t=j; t<=c; t++)
                        dp[t]=dp[t-j]+i>dp[t]?dp[t-j]+i:dp[t];
                    continue;
                }
                //处理该物品
                k=1;
                sum=map[i][j];//该物品的数量
                ////////
                while(k<sum)//寻找合适的sum
                {
                    for(t=c; t>=j*k; t--)
                        dp[t]= dp[ t- j*k ]+i*k > dp[t] ? dp[ t- j*k ]+i*k : dp[t] ;
                    sum-=k;
                    k=k*2;
                }
                for(t=c; t>=sum*j; t--)
                    dp[t]=dp[t-j*sum] + i*sum > dp[t] ? dp[t-j*sum] + i*sum : dp[t];
            }
        printf("%d\n",dp[c]);
    }
    return 0;
}
Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language
83659 2011-07-31 22:37:36 Accepted 3.3.3 281MS 212K 1679B G++

你可能感兴趣的:(HDU ACM Steps 3.3.3 Ahui Writes Word || HDU 3732 Ahui Writes Word)