【背包专题】F - Ahui Writes Word hdu3732【01背包+二进制优化】

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.

InputThe 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) 
OutputOutput 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)”

题意:给你n个物品的价值和费用,在给定的总费用内输出最大价值。

思路:数据过大,01背包tle,由于题目给出了(0 ≤ Vi , Ci ≤ 10)这一条件,说明有大量价值和费用重复,我们可以考虑多重背包,也可以用二进制数优化后转为01背包,数量的存储可以用一个二维数组进行存储。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define N 10010
int dp[N],w2[N],v2[N],value[N*10],weight[N*10],num[20][20];
char ch[20];
int n,v;
int main()
{
    int i,k,j,ans,x,y;
    while(scanf("%d%d",&n,&v)!=EOF)
    {
        memset(num,0,sizeof(num));
        for(i = 1; i <= n; i ++)
        {
            scanf("%s%d%d",ch,&value[i],&weight[i]);
            num[value[i]][weight[i]]++;
        }
        memset(dp,0,sizeof(dp));
        ans = 0;
        for(i = 1; i <= n; i ++)
        {
            x = value[i],y = weight[i];
            for(j = 1; num[x][y]>0; j*=2)
            {
                k = min(j,num[x][y]);
                w2[++ans] = k*y;
                v2[ans] = k*x;
                num[x][y]-= k;
            }
        }
        for(i = 1; i <= ans; i ++)
        {
            for(j = v; j >= w2[i]; j --)
                dp[j] = max(dp[j],dp[j-w2[i]]+v2[i]);
        }
        printf("%d\n",dp[v]);
    }
    return 0;
 } 
时间: 2024-12-14 18:46:32

【背包专题】F - Ahui Writes Word hdu3732【01背包+二进制优化】的相关文章

HDU 3732 Ahui Writes Word(多重背包)

http://acm.hdu.edu.cn/showproblem.php?pid=3732 题意: 初始有N个物品, 每个物品有cost[i]花费和val[i]价值, 你有m元钱, 现在问你最多能买多少总价值的物品? 其中N<=10W, m<=1W. 且cost[i]和val[i]都在[0,10]范围. 分析: 本题初看直接用01背包来做是直观的想法. 但是考虑到01背包的复杂度为O(N*m), 这么大的复杂度肯定不行. 然后我们发现其实每种物品只与它的cost[i]和val[i]有关, 如

HDU 3732 Ahui Writes Word

乍一看是01背包,但是100000*10000的复杂度肯定是TLE的,但是(0 ≤ Vi , Ci ≤ 10),所以最多也只有100种物品,转化成多重背包去做. #include<cstdio> #include<cstring> #include<cmath> int N,C,vv,cc; int g[15][15]; char s[100]; int w[105],c[105],n1[105]; int tot; int f[10000+10]; int max(i

【HDOJ】3732 Ahui Writes Word

初看01背包,果断TLE.是因为n和C都比较大.但是vi和ci却很小,转化为多重背包. 1 #include <cstdio> 2 #include <cstring> 3 4 int map[15][15]; 5 int dp[10005]; 6 int n, C; 7 8 int max(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void ZeroOnePack(int v, int c) { 13 for (int i

【背包专题】 D - Coins hdu2844【多重背包】

Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he kn

(转)01背包

文章作者:Yx.Ac   文章来源:勇幸|Thinking (http://www.ahathinking.com)   转载请注明,谢谢合作. --- 四月份还没写,不能这么荒废了呀,赶紧水一篇吧,哈哈.前些日子回顾了DP的一些基础,就做一下整理吧,从0-1背包开始. 本节回顾0-1背包的基本模型,关于它的实现有很多种写法,这里对不同实现做个简单列举,主要是写代码练手了,主要有以下几方面内容: ==0-1背包问题定义 & 基本实现 ==0-1背包使用滚动数组压缩空间 ==0-1背包使用一维数组

bzoj4457: 游戏任务--双层01背包

这题和NOIP的金明的预算方案(?)很像,只不过附件的数量增多了 如果对主件进行一次01背包,再套一层附件的01背包O(n4)肯定会爆.. 所以我们可以先预处理出,对于每个主件,花的时间为k的情况下,最大的经验值,用01背包做 然后再对每个主件进行01背包,这样就去掉了一层循环 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #define maxn 102 5 using namespa

CodeforcesF2. Complete the Projects (hard version) (贪心+贪心+01背包)

题目链接:传送门 思路: 对于对rating有提升的项目,肯定做越多越好,所以把$b_{i} >= 0$的项目按rating要求从小到大贪心地都做掉,得到最高的rating记为r. 对于剩余的$b_{i} < 0$的项目,因为r的范围很小,在6e4的亚子,可以考虑用01背包来做. 但是直接上01背包会WA,是因为不同项目选择的先后顺序会对结果有影响. 比如现在的r是5,有两个项目,(ai,bi)分别为(3,-3)和(3,-1),如果先做前面的项目,就会导致rating不够做后一个项目. 考虑任

01背包&amp;完全背包

·01背包&完全背包基础 01背包模型:给定n个物品,第i个物品体积为Wi,价值为Vi,背包容量为sum,选择一些物品放入背包,要求总价值最大. F[i,j]表示前i个物品放入容量为j的包里获得的最大价值. 对于任意一个物品都有两种状态,要么放要么不放,不放的话很显然价值同前,放的话就要从包里拿出一部分体积. 完全背包模型:给定n种物品,第i个物品体积为Wi,价值为Vi,背包容量为sum,选择一些物品放入背包,要求总价值最大. F[i,j]表示前i种物品放入容量为j的包里获得的最大价值. 01背

hdu 3339(最短路+01背包)

In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5135    Accepted Submission(s): 1710 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan Project t