link:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3623

看起来像完全背包,但是物品价值是变化的,所以很多人搞的很复杂。

晚上的代码要么很复杂,有一个代码虽然很简洁在zoj可以过,但是是错误的。求教lyl神犇,果然思想很深刻,抓住乐问题的本质,想法比网上搜到的所有博客里面的做法都简洁。

事实上,就是简单的DP,抓住一个技巧:让时间倒流,也就是说,把时间反过来考虑,先在将来把船造好,然后在过去用船攻击,哈哈,太巧秒了,说起来很别扭,很有意思,dp[j+time[i]]=max(dp[j]+j*time[i]);dp[j]表示在j这个时间,所造成的最大伤害。这样就可以枚举时间,在每个特定的时间内,枚举船的种类,找到最大值。最终在dp[]数组里面找到符合条件的并且时间最少的解。

只能说,ORZ……

后来好不容易想明白了。茶具从哪里来……

 /*
ID: zypz4571
LANG: C++
TASK: battle.cpp
*/ #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[][] = {{-,},{,},{,-},{,},{-,-},{-,},{,-},{,}};
using namespace std;
int Time[],dam[], f[];
int main ( int argc, char *argv[] )
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
int n,l;
while (~scanf("%d%d",&n,&l)){
for (int i=;i<=n;++i) scanf("%d%d",Time+i,dam+i);
memset(f,,sizeof(f)); f[]=;
for (int j=;j<=;++j) {
for (int i=;i<=n;++i) {
f[j+Time[i]]=max(f[j+Time[i]],f[j]+j*dam[i]);
}
}
int ans=INF;
for(int i=;i<=;++i) {
if(f[i]>=l&&ans>i) ans=i;
}
printf("%d\n",ans);
}
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */

学算法重在理解,思考,而不是套模板什么的。理解了就不用记忆了。

比如这道题目,一看,就很想完全背包,然后就搞的很复杂的样子……

其实,这道题目还有一个比较聪明的解法,因为时间最多只有300+,所以我们可以枚举时间,二分答案,对每个时间判断是否合法就OK,判断的时候,按照时间正序排就行,lyl神犇给讲的,好吧,我不会写……

做完这道题目觉得动态规划很有意思。

zoj3623 Battle Ships ——完全背包?简单DP!|| 泛化背包的更多相关文章

  1. ZOJ3623:Battle Ships(全然背包)

    Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense ...

  2. zoj3623 Battle Ships

    Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense ...

  3. POJ 1947 - Rebuilding Roads 树型DP(泛化背包转移)..

    dp[x][y]表示以x为根的子树要变成有y个点..最少需要减去的边树... 最终ans=max(dp[i][P]+t)  < i=(1,n) , t = i是否为整棵树的根 > 更新的时 ...

  4. POJ 1155 - TELE 树型DP(泛化背包转移)..

    dp[x][y]代表以x为根的子树..连接了y个终端用户(叶子)..所能获得的最大收益... dp[x][ ]可以看成当根为x时..有个背包空间为0~m...每个空间上记录了到到达这个空间的最大收益. ...

  5. HDOJ 1561 - 树形DP,泛化背包

    刚看题...觉得这不是棵树...可能有回路...仔细一想..这还真是棵树(森林)...这是由于每个城堡所需要提前击破的城堡至多一个..对于一个城堡.其所需提前击破的城堡作为其父亲构图.... dp[k ...

  6. 01背包----简单DP

    描述 且说上一周的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励的时刻了! 小Ho现在手上有M张奖券,而奖品区有N件奖品,分别标号为1到N,其中第i件奖品需要ne ...

  7. ZOJ 3623 Battle Ships DP

    B - Battle Ships Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

  8. Battle Ships(复习泛化物品**)

    传送门Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which i ...

  9. [ZOJ 3623] Battle Ships

    Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which is s ...

随机推荐

  1. lucene 过滤结果

    package cn.itcast.h_filter; import java.util.ArrayList; import java.util.List; import org.apache.luc ...

  2. SA 的参数

    SA 的参数也只能是常数数组. http://www.cnblogs.com/del/archive/2009/10/27/1590692.html ja := SA([]); jo := SO(); ...

  3. JS编码,解码. asp.net(C#)对应解码,编码

    escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@ ...

  4. Visual studio 2013的安装和单元测试

    vs2013安装过程: 简单的单元测试: 1.创建新的c#类库 2.创建单元测试 3.测试结果 单元测试结束

  5. mybatis ForEach Collection集合等规范解析(转)

    转自:http://blog.csdn.net/wj3319/article/details/9025349 在SQL开发过程中,动态构建In集合条件查询是比较常见的用法,在Mybatis中提供了fo ...

  6. Linear Predictors

    In this chapter we will study the family of linear predictors, one of the most useful families of hy ...

  7. WEB-INF& 绝对路径vs相对路径

    在struts2里面用XXXInput方法和springmvc里面用GET模式进入添加或者登陆输入页面,有时候会在方法里面做一下准备,比如动态读入部门信息放在select中,或者在model中加入一个 ...

  8. 在iis6.0公布asp.net mvc3网站

    在iis6.0发布asp.net mvc3网站 这个问题一直困扰了我很长一段时间,终于在今天综合网上查的资料把它解决了. 在iis6.0发布asp.net mvc3网站 1   需要在服务器下安装.n ...

  9. C#基础--面向对象计算器

    static void Main(string[] args) { //new个对象 Calculator cal = new Calculator(); //做事情 cal.Run(); } /// ...

  10. 使用HackRF+GNU Radio 破解吉普车钥匙信号

    引文 我最近对软件定义的无线电技术(SDR)产生了浓厚的兴趣,而我对其中一款流行的SDR平台(HackRF)也产生了兴趣,而其频率接收的范围也在1MHz ~6GHz之间(范围较广).而这里也需要提及一 ...