ZOJ 3623 - Battle Ships(完全背包)
来源:互联网 发布:雪肌精乳液好用吗 知乎 编辑:程序博客网 时间:2024/05/16 17:44
Description
Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which hasL longevity. The player has a military factory, which can produce N kinds of battle ships. The factory takes ti seconds to produce thei-th battle ship and this battle ship can make the tower loss li longevity every second when it has been produced. If the longevity of the tower lower than or equal to 0, the player wins. Notice that at each time, the factory can choose only one kind of battle ships to produce or do nothing. And producing more than one battle ships of the same kind is acceptable.
Your job is to find out the minimum time the player should spend to win the game.
Input
There are multiple test cases.
The first line of each case contains two integers N(1 ≤ N ≤ 30) andL(1 ≤ L ≤ 330), N is the number of the kinds of Battle Ships,L is the longevity of the Defense Tower. Then the following N lines, each line contains two integerst i(1 ≤ t i ≤ 20) and li(1 ≤li ≤ 330) indicating the produce time and the lethality of the i-th kind Battle Ships.
Output
Output one line for each test case. An integer indicating the minimum time the player should spend to win the game.
Sample Input
1 11 12 101 12 53 1001 103 2010 100
Sample Output
245
思路:
一开始的想法是将破坏值作为dp数组的下标,但是死活无法得到正确答案,思路是很有漏洞的。
应该转化思路,将时间作为dp 的下标,dp[i] 表示在时间i 时造成的破坏值。状态转移方程是:dp[j] = max(dp[j] , dp[j - t[i]] + (j - t[i])*l[i] );最后一个循环枚举时间(1<=i<=600)输出dp[i]第一个值大于等于p 的i值。
CODE:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int inf = 0xffffff;int t[35], l[35], dp[400];int main(){ //freopen("in", "r", stdin); int n, p; while(~scanf("%d %d", &n, &p)){ memset(dp, 0, sizeof(dp)); for(int i = 0; i < n; ++i){ scanf("%d %d", &t[i], &l[i]); } for(int i = 0; i < n; ++i){ for(int j = t[i]; j <= t[i] + p; ++j){ dp[j] = max(dp[j], dp[j - t[i]] + (j - t[i]) * l[i]); } } for(int i = 0; i <= 660; i++){ if(dp[i] >= p){ printf("%d\n", i); break; } } } return 0;}
- ZOJ 3623 Battle Ships(完全背包)
- ZOJ 3623 - Battle Ships(完全背包)
- ZOJ 3623 Battle Ships (完全背包)
- ZOJ 3623 Battle Ships(完全背包)
- ZOJ-3623 Battle Ships (完全背包 应用题)
- TOJ 4365 ZOJ 3623 Battle Ships / 完全背包
- ZOJ - 3623 Battle Ships(完全背包或者BFS)
- Battle Ships(完全背包)
- 完全背包 battle ships
- ZOJ3623:Battle Ships(完全背包)
- ZOJ 3623 Battle Ships
- zoj 3623 Battle Ships
- ZOJ 3623 Battle Ships
- ZOJ 3623 Battle Ships
- ZOJ 3623 Battle Ships
- zoj 3623 battle ships
- ZOJ 3623 Battle Ships
- ZOJ - 3623 - Battle Ships (01背包变形)
- 一元二次方程
- TRIZ系列-创新原理-36-状态转变原理
- python:文字识别 (转自 新浪博客 恬月园)
- 数据库面试题
- android基础学习之AsyncTask
- ZOJ 3623 - Battle Ships(完全背包)
- 编程如何获得通过草图创建的Revit构件关联的原始草图对象
- 【tyvj1863】 [Poetize I]黑魔法师之门
- [leetcode]Remove Duplicates from Sorted List
- hdu 1465(不容易系列之一)(水题,错排公式)(a[n]=(n-1)*(a[n-1]+a[n-2]))
- VC在debug版和release版中分配堆的区别
- 利用java生成不重复的ID
- 2014.10.22面试总结
- DEDECMS无法上传图片的解决办法