HDOJ 3732

Ahui Writes Word

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


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
 
01背包TLE,后来才得知可以多重背包。
物品个数很大,背包的体积很大,但是物品的价值和花费都很小。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <iomanip>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1|1
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
#define mid ((l + r) >> 1)
#define mk make_pair
const int MAXN = 100000 + 50;
const int maxw = 100 + 20;
const int MAXNNODE = 1000 +10;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 10007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
const D e = 2.718281828459;
int dp[MAXN];
int n , m , a , v;
int cnt[11][11];///用于统计相同价值和费用的单词的数目

char str[30];
int main()
{
    //ios::sync_with_stdio(false);
#ifdef Online_Judge
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif // Online_Judge
    while(~scanf("%d%d" , &n , &m))
    {
        clr(cnt , 0);
        FOR(i , 0 , n)
        {
            scanf("%s%d%d" , str ,& a , &v);
            cnt[a][v]++;
        }
        clr(dp , 0);
        FOR(i , 0 , 11)FOR(j , 0 , 11)
        {
            if(cnt[i][j] == 0)continue;
            if(cnt[i][j] * j >= m)///完全背包 
            {
                FORR(k , j , m)dp[k] = max(dp[k] , dp[k - j] + i);
            }
            else///01背包
            {
                int l = 1;
                while(l < cnt[i][j])
                {
                    REPP(k , m , l * j)
                    {
                        dp[k] = max(dp[k] ,  dp[k - l * j] + l * i);
                    }
                    cnt[i][j] -= l;
                    l <<= 1;
                }
                REPP(k , m , cnt[i][j] * j)dp[k] = max(dp[k] , dp[k - cnt[i][j] * j] +  cnt[i][j] * i);
            }
        }
        printf("%d\n" , dp[m]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值