zoj3623 Battle Ships ——完全背包?简单DP!|| 泛化背包
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!|| 泛化背包的更多相关文章
- ZOJ3623:Battle Ships(全然背包)
Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense ...
- zoj3623 Battle Ships
Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense ...
- POJ 1947 - Rebuilding Roads 树型DP(泛化背包转移)..
dp[x][y]表示以x为根的子树要变成有y个点..最少需要减去的边树... 最终ans=max(dp[i][P]+t) < i=(1,n) , t = i是否为整棵树的根 > 更新的时 ...
- POJ 1155 - TELE 树型DP(泛化背包转移)..
dp[x][y]代表以x为根的子树..连接了y个终端用户(叶子)..所能获得的最大收益... dp[x][ ]可以看成当根为x时..有个背包空间为0~m...每个空间上记录了到到达这个空间的最大收益. ...
- HDOJ 1561 - 树形DP,泛化背包
刚看题...觉得这不是棵树...可能有回路...仔细一想..这还真是棵树(森林)...这是由于每个城堡所需要提前击破的城堡至多一个..对于一个城堡.其所需提前击破的城堡作为其父亲构图.... dp[k ...
- 01背包----简单DP
描述 且说上一周的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励的时刻了! 小Ho现在手上有M张奖券,而奖品区有N件奖品,分别标号为1到N,其中第i件奖品需要ne ...
- ZOJ 3623 Battle Ships DP
B - Battle Ships Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- Battle Ships(复习泛化物品**)
传送门Battle Ships Time Limit: 2 Seconds Memory Limit: 65536 KB Battle Ships is a new game which i ...
- [ZOJ 3623] Battle Ships
Battle Ships Time Limit: 2 Seconds Memory Limit: 65536 KB Battle Ships is a new game which is s ...
随机推荐
- select2去除搜索框
$("#type_select").select2({ minimumResultsForSearch: -1 });
- 兼容 IE,firfox 的时间日期出现 NaN
//当前日期加上天数后的新日期.function AddDays(days) { var d = new Date(); var year = d.getFullYear(); var day = ...
- Rhel6-heartbeat+lvs配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 192.168.12 ...
- 蓝桥杯 algo——6 安慰奶牛 (最小生成树)
问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是一个奶牛的家.FJ计 划除去P条道路中尽可能多的道路 ...
- poj2264 dp+路径
//Accepted 208K 0MS //dp //最长公共子序列+路径 #include <cstdio> #include <cstring> #include < ...
- 表单验证与Json配合
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- STL 源码分析《1》---- list 归并排序的 迭代版本, 神奇的 STL list sort
最近在看 侯捷的 STL源码分析,发现了以下的这个list 排序算法,乍眼看去,实在难以看出它是归并排序. 平常大家写归并排序,通常写的是 递归版本..为了效率的考虑,STL库 给出了如下的 归并排序 ...
- BZOJ 1816 扑克牌
WA的我怀疑人生.. 发现原来是循环中间就要break掉,不然爆int. 总感觉这题可以直接构造啊.. #include<iostream> #include<cstdio> ...
- java IO流的体系结构图
常用字节流字符流 字节流 InputStream ...
- IOS开发-几种截屏方法
IOS开发-几种截屏方法 1. UIGraphicsBeginImageContextWithOptions(pageView.page.bounds.size, YES, zoomSc ...