这是一道背包题,当你题读完了的时候,你会觉得这道题明明就是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的更多相关文章

  1. HDU 3732 Ahui Writes Word(多重背包)

    HDU 3732 Ahui Writes Word(多重背包) http://acm.hdu.edu.cn/showproblem.php? pid=3732 题意: 初始有N个物品, 每一个物品有c ...

  2. HDU 3732 Ahui Writes Word 多重背包优化01背包

    题目大意:有n个单词,m的耐心,每个单词有一定的价值,以及学习这个单词所消耗的耐心,耐心消耗完则,无法学习.问能学到的单词的最大价值为多少. 题目思路:很明显的01背包,但如果按常规的方法解决时间复杂 ...

  3. hdoj 3732 Ahui Writes Word (多重背包)

    之前在做背包的题目时看到了这道题,一看,大喜,这不是裸裸的01背包吗!!  然后华丽丽的超时,相信很多人也和我一样没有考虑到数据量的大小. 时隔多日,回过头来看这道题,依旧毫无头绪....不过相比之前 ...

  4. 3732 Ahui Writes Word

    // N个物品 放进容量为C的背包里面 要求价值最大// 一看 第一反应是0 1背包 不过 N=100000 C=10000// 注意到 v,c在 10以内// 那么 最多就100种组合了 然后就转化 ...

  5. 【HDOJ】3732 Ahui Writes Word

    初看01背包,果断TLE.是因为n和C都比较大.但是vi和ci却很小,转化为多重背包. #include <cstdio> #include <cstring> ][]; ]; ...

  6. Ahui Writes Word

    Ahui Writes Word Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...

  7. hdu 3732

    #include<stdio.h> #include<string.h> int n,m,dp[10001]; int max(int a,int b) {  return a ...

  8. 【二进制拆分多重背包】【HDU1059】【Dividing】

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  9. HDU3732 背包DP

    Ahui Writes Word Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. 【转】目前最细致清晰的NSDictionary以及NSMutableDictionary用法总结 -- 不错

    原文网址:http://www.cnblogs.com/wengzilin/archive/2012/03/15/2397712.html 做过Java语言 或者 C语言 开发的朋友应该很清楚 关键字 ...

  2. 【转】Android中visibility属性VISIBLE、INVISIBLE、GONE的区别

    原文网址:http://blog.csdn.net/chindroid/article/details/8000713 在Android开发中,大部分控件都有visibility这个属性,其属性有3个 ...

  3. MyEclipse 下用link 方式安装插件

      N年之前,也是Java出身,后来转DBA,Java 就忘的差不多了. 最近计划好好研究下Python,还是选用MyEclipse 这个IDE了,多年没敲代码,很多东西都忘记了,做点笔记,备用. M ...

  4. VTK三维重建(2)-根据脚部骨骼CT的三维重建和显示

    [效果演示] 根据脚部的骨骼CT扫描的照片,利用VTK完成读取和三维重建. [程序实现] void main () { vtkRenderer *aRenderer = vtkRenderer::Ne ...

  5. Anti-Grain Geometry 概述

    AGG是一个轻量.灵活.可靠的图形算法库,AGG各部分之间是松耦合的,也即是说各部分可以单独使用. The primary goal of Anti-Grain Geometry is to brea ...

  6. uva 11991 Easy Problem from Rujia Liu? vector+map

    水题 学习一下数据的存储方法. #include<iostream> #include<cstdio> #include<cstdlib> #include< ...

  7. Tasks on 2013

    1.改进并补充实验 2.样本选取和文档分布调查 3. Diversity Metrics 4. PPT for Project starting 1. Chrome & webkit 2. O ...

  8. tomcat6-7配置管理用户

    tomcat6: <?xml version='1.0' encoding='utf-8'?> <tomcat-users>   <role rolename=" ...

  9. Visual Studio 2008 – ASP.NET “System.Runtime.InteropServices.COMException”

    The Issue When openning an existing ASP.NET project for the first time in Visual Studio 2008 it retu ...

  10. ASP.NET SignalR2持久连接层解析

    越是到年底越是感觉浑身无力,看着啥也不想动,只期盼着年终奖的到来以此来给自己打一针强心剂.估摸着大多数人都跟我一样犯着这样浑身无力的病,感觉今年算是没挣到啥钱,但是话也不能这么说,搞得好像去年挣到钱了 ...