Luo Gu P1616 crazy herbs

Ok...

 

Topic link: https: //www.luogu.org/problemnew/show/P1616

 

First, the positive solution of this question is: Full backpack.

 

First, a backpack?

  Backpack categories:

  Backpack is divided into 01 bags, backpacks and multiple backpack full of these three basic models, the other three kinds of knapsack problems are from out of the backpack extension application.

  This section describes: This question adapted from  <P1048>采药- the portal .

  This question is what the backpack?

  At first sight the question surfaces: [无限量]picking medicine, this is totally a backpack.

  Template title backpack full face is this: there are n kinds of items, each item has a weight and a value. But the number of each item is infinite, while a backpack,

  The maximum load is M, this is selected from the n kinds of articles of several pieces (unlimited select the same article), so that weight and less than or equal M, with a maximum value and.

  A feature of this problem we see with the above description of the co-owners, each of an unlimited number of items. So we only need 01 backpack code slightly modified. (01 Multi-turn only slightly modified)

 

Second, the code?

  Backpack full code snippet:

1 for(int i = 1;i <= n; i++)
2     for(int j = w[i]; j <= V; j++)
3         dp[j] = max(dp[j], f[j - w[i]] + c[i]);

  01 backpack snippet:

1 for(int i = 1; i <= n; i++)
2     for(int j = m; j >= s[i]; j--)
3         dp[j] = max( dp[j], dp[j- s[i]] + v[i]);

  the difference?

  The reverse change order, because we need to change the status of the recurrence of the current drugs after the update. So to solve the order.

 

AC Code:

 1 #include<cstdio>
 2 #include<iostream> 
 3 
 4 using namespace std;
 5 
 6 const int maxn = 100005;
 7 
 8 int ti[maxn], dp[maxn], val[maxn];
 9 int ans;
10 
11 int main(){
12     int t, m;
13     scanf("%d%d", &t, &m);
14     for(int i = 1; i <= m; i++)
15         scanf("%d%d", &ti[i], &val[i]);
16     for(int i = 1; i <= m; i++){
17         for(int j = ti[i]; j <= t; j++){
18             dp[j] = max(dp[j - ti[i]] + val[i], dp[j]);
19         }
20     }
21     for(int i = 1; i <= t; i++)
22         ans = max(ans, dp[i]);
23     printf("%d\n", ans);
24      return  0 ;
25 }
AC Code

 

Guess you like

Origin www.cnblogs.com/New-ljx/p/11183993.html