HDU 3732 Ahui Writes Word

Ahui Writes Word

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2856    Accepted Submission(s): 1018


Problem Description
We all know that English is very important, so Ahui strive for this in order to learn more English words. To know that word has its value and complexity of writing (the length of each word does not exceed 10 by only lowercase letters), Ahui wrote the complexity of the total is less than or equal to C.
Question: the maximum value Ahui can get.
Note: input words will not be the same.
 
Input
The first line of each test case are two integer N , C, representing the number of Ahui’s words and the total complexity of written words. (1 ≤ N ≤ 100000, 1 ≤ C ≤ 10000)
Each of the next N line are a string and two integer, representing the word, the value(Vi ) and the complexity(Ci ). (0 ≤ Vi , Ci ≤ 10)
 
Output
Output the maximum value in a single line for each test case.
 
Sample Input
5 20 go 5 8 think 3 7 big 7 4 read 2 6 write 3 5
 
Sample Output
15
Hint
Input data is huge,please use “scanf(“%s”,s)”
 
Author
Ahui
 
Source
 
Recommend
notonlysuccess   |   We have carefully selected several similar problems for you:  3730 2546 3739 3496 3735 
/*
二进制优化的时候,转化成完全背包就错,注释掉就对。神奇
*/
#include<bits/stdc++.h>
#define N 10005
#define M 11000
using namespace std;
int dp[M];

void ZeroOne_Pack(int cost,int weight,int n)
{
    for(int i=n;i>=cost;i--)
        dp[i]=max(dp[i],dp[i-cost]+weight);
}

void Complete_Pack(int cost,int weight,int n)
{
    for(int i=cost;i<=n;i++)
        dp[i]=max(dp[i],dp[i-cost]+weight);
}

int Multi_Pack(int c[],int w[],int num[],int n,int m)
{
    memset(dp,0,sizeof (dp));
    for(int i=1;i<=n;i++)
    {
        // if(w[i]*num[i]>m)
        // {
            // Complete_Pack(c[i],w[i],m);
        // }    
        // else
        // {
            //cout<<i<<endl;
            int k=1;
            while(k<num[i])//进行二进制拆分
            {
                ZeroOne_Pack(c[i]*k,w[i]*k,m);
                num[i]-=k;
                k<<=1;
            }
            ZeroOne_Pack(c[i]*num[i],w[i]*num[i],m);
        // }
    }
    return dp[m];
}
char op[20];
int main()
{
    //freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
    int n,m;
    int val,weight;
    int num[15][15];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(num,0,sizeof num);
        for(int i=1;i<=n;i++)
        {
            scanf("%s%d%d",op,&weight,&val);
            num[weight][val]++;
        }    
        int Num[N],V[N],W[N];
        memset(V,0,sizeof V);
        memset(W,0,sizeof W);
        memset(Num,0,sizeof Num);
        int len=0;
        for(int i=0;i<=10;i++)
            for(int j=0;j<=10;j++)
            {
                if(num[i][j]>0)
                {
                    //cout<<num[i][j]<<endl;
                    len++;
                    Num[len]=num[i][j];
                    V[len]=j;
                    W[len]=i;
                    //cout<<W[len]<<" "<<V[len]<<endl;
                }
            }
        //cout<<len<<endl;
        printf("%d
",Multi_Pack(V,W,Num,len,m));
        // for(int i=0;i<=m;i++)
            // cout<<dp[i]<<" ";
        // cout<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5994260.html