hdu 3732 Ahui Writes Word
来源:互联网 发布:dj网络歌手 编辑:程序博客网 时间:2024/05/16 13:46
这是一道背包题,当你题读完了的时候,你会觉得这道题明明就是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
- hdu 3732 Ahui Writes Word
- hdu 3732 Ahui Writes Word
- HDU-3732 Ahui Writes Word
- HDU 3732 Ahui Writes Word 解题报告
- HDU-3732 Ahui Writes Word 解题报告
- HDU 3732 Ahui Writes Word(多重背包)
- HDU 3732(Ahui Writes Word)多重背包
- HDU - 3732 Ahui Writes Word(多重背包)
- HDU ACM Steps 3.3.3Ahui Writes Word || HDU 3732 Ahui Writes Word
- HDOJ 3732 Ahui Writes Word
- HDOJ 3732 Ahui Writes Word
- hdu 3732 Ahui Writes Word 多重背包 小心超时
- HDU 3732 Ahui Writes Word 经典动态规划 DP
- hdu 3732 Ahui Writes Word(简单dp+贪心)
- HDU 3732 Ahui Writes Word(多重背包问题)
- Quartz CronTrigger最完整配置说明
- Hadoop架构、部署及编程
- cocosBuilder创建类映射调用遇到的问题
- OC正式协议和非正式协议
- 杭电oj2004
- hdu 3732 Ahui Writes Word
- Ubuntu13.04 常用命令备忘
- java 简单的文件上传下载服务器模型(NIO 版本)
- 揭开Socket编程的面纱
- Secure CRT 自动记录日志 配置 小记
- 师夷长技以制夷
- 什么时候用单向关联和双向关联
- poj(3186)Treats for the Cows
- UVA 10564 - Paths through the Hourglass (dp)