Battle Ships

来源:互联网 发布:java构建工具ant 编辑:程序博客网 时间:2024/04/30 11:32

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 produceN kinds of battle ships. The factory takes ti seconds to produce thei-th battle ship and this battle ship can make the tower lossli 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 followingN 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做,但是问题是这里每艘船在生产出来后每一秒都对tower造成伤害,由于之前看过很多的关于dp的解释,说子问题不能有后效性,就是对各个子问题寻求最优解,最后就能得到整个问题的最优解,但是我思来想去,这题算不算有后效性呢?于是一直卡着。

一开始想过这是完全背包,然后定义dp[x]:到第x秒能够产生的最大伤害。

但是问题依旧存在,就是状态转移方程不知道如何求:

dp[x]=max(dp[ x - t[ i ] ? ] + ?,dp[x]);

然后就一直想一直想。

终于想不出来,只能百度,一看,状态转移方程式这样的:

dp[x]=max(dp[ x - t[ i ]  ] + (j - t[i])*tt[i] , dp[x]);

然后仔细一想,对了,(首先在这里自嘲一下。。。自己的能力真的是不够,对于一个问题的想法有限,发散思维不够强。。。这点,现在还没想到怎么破解,但是至少我知道每个题目的解法都是思路的一个体现,而思路大多是从一个人看事物的方式决定的,我觉得可能多看书,多理解,多发掘,可能对于这部分能力会有所提升,总之多看书多打码)

这个状态转移方程怎么来的?我们总是习惯背包的模式,一个背包叠加一个背包,减掉一个背包重量再加上一个背包价值,但是背包的前置和后置是没有关系的,而这题却不是

所以在第i种船的建造时,我们肯定将这第i种船添加到最前面去生产,然后乘以时间(减去生产时间)加上减去生产时间所能造成最大伤害与该时间的伤害比较,就能得出结果了。。

贴下代码

#include <stdio.h>#include <iostream>#include <cstring>#include <stdlib.h>#include <algorithm>#include <queue>#include <math.h>#define INF 1e8using namespace std;int n,l;int t[40],tt[40];int dp[300];int main(void){    while (scanf("%d %d",&n,&l)!=EOF)    {        for (int i = 0;i < n;i++)        {            scanf("%d %d",&t[i],&tt[i]);        }        memset(dp,0,sizeof(dp));        for (int i = 0;i < n;i++)        {            for (int j = t[i];j < 300;j++)            {                dp[j]=max(dp[j],dp[j-t[i]]+(j-t[i])*tt[i]);                if(dp[j]>=l)                    break;            }        }        for (int i = 0;i < 300;i++)        {            if(dp[i]>=l)            {                cout<<i<<endl;                break;            }        }    }    return 0;}


cheer up!!!


0 0