hdu 3732 Ahui Writes Word
这是一道背包题,当你题读完了的时候,你会觉得这道题明明就是01背包的完全版吗!
no no no no no no no no no no no~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
~~~~~~~~~~~~~~~~~~~~~
对!不是,是,还是不是,是~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
好吧,这是一道01背包题,但按01背包做会超时,我之所以知道是因为我做过按01背包!
这道题的妙处在于转化为多重背包!,对,这就是这道题的妙处!我喜欢这道题的这一点!!!!
背包好久没看了!哎····,也忘得差不多了!!!
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><<><<K<<><><><><><><<><><><><><><><><><><><<
#include<stdio.h>
#include<string.h>
#define max(a,b) a>b?a:b
int bb[500010],q,vv;
void shun(int cost,int weight)
{
int i;
for(i=cost;i<=vv;i++)
bb[i]=max(bb[i],bb[i-cost]+weight);
}
void ni(int cost,int weight)
{
int i;
for(i=vv;i>=cost;i--)
bb[i]=max(bb[i],bb[i-cost]+weight);
}
int main()
{
char str[5000];
int n,w[5001],v[5001],i,j,amount[5001],v1,w1,k;
while(scanf("%d %d",&n,&vv)!=EOF)
{
memset(amount,0,sizeof(amount));
q=0;
for(i=1;i<=n;i++)
{
scanf("%s %d %d",str,&w1,&v1);
for(j=0;j<q;j++)
if(w1==w[j]&&v1==v[j])
{
amount[j]++;
break;
}
if(j==q)
{
w[q]=w1;v[q]=v1;
amount[q]=1;
q++;
}
}
memset(bb,0,sizeof(bb));
for(i=0;i<q;i++)
{
if(v[i]*amount[i]>=vv)
shun(v[i],w[i]);
else
{
k=1;
while(k<=amount[i])
{
ni(k*v[i],k*w[i]);
amount[i]-=k;
k*=2;
}
ni(amount[i]*v[i],amount[i]*w[i]);
}
}
printf("%d\n",bb[vv]);
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3732
hdu 3732 Ahui Writes Word的更多相关文章
- HDU 3732 Ahui Writes Word(多重背包)
HDU 3732 Ahui Writes Word(多重背包) http://acm.hdu.edu.cn/showproblem.php? pid=3732 题意: 初始有N个物品, 每一个物品有c ...
- HDU 3732 Ahui Writes Word 多重背包优化01背包
题目大意:有n个单词,m的耐心,每个单词有一定的价值,以及学习这个单词所消耗的耐心,耐心消耗完则,无法学习.问能学到的单词的最大价值为多少. 题目思路:很明显的01背包,但如果按常规的方法解决时间复杂 ...
- hdoj 3732 Ahui Writes Word (多重背包)
之前在做背包的题目时看到了这道题,一看,大喜,这不是裸裸的01背包吗!! 然后华丽丽的超时,相信很多人也和我一样没有考虑到数据量的大小. 时隔多日,回过头来看这道题,依旧毫无头绪....不过相比之前 ...
- 3732 Ahui Writes Word
// N个物品 放进容量为C的背包里面 要求价值最大// 一看 第一反应是0 1背包 不过 N=100000 C=10000// 注意到 v,c在 10以内// 那么 最多就100种组合了 然后就转化 ...
- 【HDOJ】3732 Ahui Writes Word
初看01背包,果断TLE.是因为n和C都比较大.但是vi和ci却很小,转化为多重背包. #include <cstdio> #include <cstring> ][]; ]; ...
- Ahui Writes Word
Ahui Writes Word Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu 3732
#include<stdio.h> #include<string.h> int n,m,dp[10001]; int max(int a,int b) { return a ...
- 【二进制拆分多重背包】【HDU1059】【Dividing】
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU3732 背包DP
Ahui Writes Word Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- (转) mysql数据库引擎:MyISAM和InnoDB(性能优化)
转自 http://yuwensan126.iteye.com/blog/1138022 Mysql 数据库中,最常用的两种引擎是innordb和myisam.Innordb的功能要比myiasm强大 ...
- iOS开发中提交带有中文或特殊字符串的参数
iOS开发中,与后台进行数据交换是一个很常见的场景. 在web开发中,对于我们提交的地址,浏览器会负责进行decode,但是在ios中,必须要自己手动来实现.否则我们拼接出的网址在包括中文.特殊字符串 ...
- VBScript: Windows脚本宿主介绍
Windows脚本宿主(Windows Script Host, WSH)是一个Windows管理工具.WSH创建了一个脚本运行的主环境,WSH使脚本能够使用对象和服务,并提供脚本执行的准则.WSH还 ...
- JDK1.5新特性(六)……Generics
概述 Generics - This long-awaited enhancement to the type system allows a type or method to operate on ...
- JDBC注册驱动的三种方式
JDBC注册驱动的三种方式 1.通过导入的JDBC的驱动包拿到的com.mysql.jdbc.Driver对象,利用java.sql.DriverManager对象的DriverManager.reg ...
- java 内部类的使用
http://www.cnblogs.com/wenruo/p/5387995.html 内部类 就是在类中嵌套的另一个类. 非静态内部类 创建内部类的方式就是把类定义在外部类里面. class Ou ...
- TCP/IP模型的简单解释
TCP/IP模型是互联网的基础.想要理解互联网,就必须理解这个模型.但是,它不好懂,我就从来没有搞懂过. 前几天,BetterExplained上有一篇文章,很通俗地解释了这个模型.我读后有一种恍然大 ...
- [iOS基础控件 - 3.1] QQ登陆界面
A.storyboard 控件版 1.label 2.textfield a.Keyboard Type 账号:Number Pad 密码:Num ...
- UI基础 获取当前屏幕显示的viewcontroller
#pragma mark - 获取当前屏幕显示的viewcontroller - (UIViewController *)getCurrentVC { UIViewController *result ...
- 读取.tmx地图
读取.tmx地图 m_GameMap = CCTMXTiledMap::create("map1.tmx"); this->addChild(m_GameMap,1); 读取 ...