ZOJ 3623 Battle Ships 造战舰炸塔 动态规划 伪装较好的完全背包 ★★★

Battle Ships

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值