zoj 3623 B

B - Battle Ships
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Submit Status

Description

Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N kinds of battle ships. The factory takes ti seconds to produce the i-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) and L(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 integers i(1 ≤ 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

这题看出是类似背包的问题...
一开始想用dp[i]表示造成i的伤害所需要的最少时间...
但是向了半天没有想出转移方程QAQ
看了题解 http://www.cnblogs.com/liuxueyang/archive/2013/08/15/3260211.html
http://m.blog.csdn.net/blog/u013303743/35988789
两份都看一下比较好。
  1. 得到的经验是:如果不好直接表示造成伤害i所需要的最少时间,其实可以反过来,表示对于时间i造成的最大伤害。最后扫一遍就行了。
  2. 倒着考虑会好做很多。而可以这样做的原因是,每一个武器在建造之后输出的时候不会影响其他武器的建造,也就是每个武器是独立的       

唉。。。DP还是太渣了QAQ

 1 /*************************************************************************
 2     > File Name: code/zoj/3623.cpp
 3     > Author: 111qqz
 4     > Email: rkz2013@126.com 
 5     > Created Time: 2015年10月19日 星期一 17时01分08秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<iomanip>
10 #include<cstdio>
11 #include<algorithm>
12 #include<cmath>
13 #include<cstring>
14 #include<string>
15 #include<map>
16 #include<set>
17 #include<queue>
18 #include<vector>
19 #include<stack>
20 #include<cctype>
21                  
22 #define yn hez111qqz
23 #define j1 cute111qqz
24 #define ms(a,x) memset(a,x,sizeof(a))
25 using namespace std;
26 const int dx4[4]={1,0,0,-1};
27 const int dy4[4]={0,-1,1,0};
28 typedef long long LL;
29 typedef double DB;
30 const int inf = 0x3f3f3f3f;
31 const int N=400; //最坏的情况是建造时间为20s,伤害为1,这样也只需要350s(20s建造,330s每s一滴血)就可以干掉330血(max)的怪物...保险一点400.
32 int n,v;
33 int t[35],l[35];
34 int dp[N];
35 int main()
36 {
37   #ifndef  ONLINE_JUDGE 
38    freopen("in.txt","r",stdin);
39   #endif
40 
41    while (scanf("%d %d",&n,&v)!=EOF)
42     {
43     for ( int i = 0 ; i < n ; i++) scanf("%d %d",&t[i],&l[i]);
44     ms(dp,0);                       //dp[i]表示到时间i能造成的最大伤害
45 
46     for ( int j = 0 ; j < N-25 ; j++)
47         for ( int i = 0; i < n ; i++)            //怒re一发,sad
48         dp[j+t[i]] = max(dp[j+t[i]],dp[j]+j*l[i]); //转移方程能这么写是因为,当某个武器建造好后,输出的时候就不影响其他了
49                                 //这样就可以倒着想,先建造,再输出。
50     for ( int i = 0 ; i < N  ; i++)                      //以前觉得背包问题一定是对于某个容量的最大价值
51                                 //但实际上在找这个最大价值的过程中,对于每个容量的最大价值都得到
52                                 
53     {
54       //  cout<<"dp[i]:"<<dp[i]<<endl;
55         if (dp[i]>=v)
56         {
57         printf("%d
",i);
58         break;
59         }
60     }
61     }
62   
63    
64  #ifndef ONLINE_JUDGE  
65   fclose(stdin);
66   #endif
67     return 0;
68 }
View Code
原文地址:https://www.cnblogs.com/111qqz/p/4892692.html