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 1 1 1 2 10 1 1 2 5 3 100 1 10 3 20 10 100
Sample Output
2 4 5
根据动态规划的无后效性,这个题目的考虑顺序要反着来,先考虑最后建的塔,然后再考虑倒数第二个。
如果先考虑第一个建立的塔,那转移将极为麻烦,还要保存建了哪些塔,而且不满足最优子结构。
实质类似完全背包,但又不完全是。
造兵耗费时间,相当于背包问题物品占用体积,而在这个题里面,造兵创造的价值却不是一个常量的,是根据时间的变化而变化。
问题问的是最少的时间,问题可以转化为从1s开始向后找,2s、3s...,看至少多长时间创造的最大伤害超过了塔的生命,即可。
状态转移方程: dp[v]=max{ dp[v], dp[v-cost[i]]+ (v-cost[i])*atk[i] };
这里有一点可以考虑到的就是,对于某个时间,一直出兵造成的最大伤害>=有空闲造成的最大伤害。
这里的dp[x]其实指的是给出x秒能造成的最大伤害,而且是不空闲地出兵,而且dp[x]的决策是倒数第x秒的决策。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson ind<<1,le,mid
#define rson ind<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk make_pair
#define _f first
#define _s second
using namespace std;
//const int INF= ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn= 30+10 ;
const int maxV= 360+10 ;
int n,V,life;
int atk[maxn];
int cost[maxn];
int dp[maxV];
int main()
{
while(~scanf("%d%d",&n,&life))
{
V=life+20;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&cost[i],&atk[i]);
}
memset(dp,0,sizeof dp);
for(int i=1;i<=n;i++)
{
for(int v=cost[i];v<=V;v++)
{
dp[v]=max(dp[v], dp[v-cost[i]]+ (v-cost[i])*atk[i]);
}
}
for(int i=1;i<=V;i++)
{
if(dp[i]>=life) {printf("%d\n",i);break;}
}
}
return 0;
}