Luo Gu P5662 Souvenirs & [NOIP2019 popular group] (dp, completely backpack)

Portal


Problem-solving ideas

This question we must first understand that in every day, the best strategy is to operate 2 (sell), before proceeding 1 (buy), is to maximize the benefits.

Obviously this question when only two days, is a full backpack, the price is the same day as the volume, the price difference between tomorrow and today's price as a value, you can run while completely backpack. The time complexity of O (TNM)

Then we consider out of practice - we dp [j] represent the best interests while remaining j volumes, can be obtained.

Then for all day behind the two-day run again completely backpack. Every time statistical answer is dp [m] + m, ie profit + principal.

Here we must pay attention to it every day, to make money to buy did not affect the way the future, so just under the record of the day the largest amount of money (that is, tomorrow the principal) can be. Dp then have to empty the array.

Another focus is the same for each item, in the first day to buy x k pieces, the first k x + 1 day sell parts, but also buy pieces of k, x + 2 days and the first k pieces to buy, in fact, this process in the first day to buy the equivalent of x k pieces, sold in pieces k x + 2 days, so we enumerate all possible unrestricted (but overcharged a few steps Bale) .

Here you can also interpret it this way (greedy): is the day to buy the goods the next day, we must all be sold. If you do not sell a deal, then come back to buy.

There is also a place more difficult to understand: Why do not we record how to buy it? In the first x number of days will not sell souvenirs do not have it?

When the answer in the negative. Why?

Look at the code, when we buy a piece of memorabilia, we add value is (price tomorrow - today's price), plus tomorrow is then sold, then the profits earned Numerically, emotional understanding, then buy it today in the next few articles necessarily all sold. So the operation is actually only 1 day of operation (buy).

Obviously, as long as enough money, you can buy an unlimited number.

AC Code

. 1 #include <the iostream>
 2 #include <cstdio>
 . 3 #include <the cmath>
 . 4 #include <CString>
 . 5  the using  namespace STD;
 . 6  const  int MAXN = 105 ;
 . 7  const  int MAXM = 10005 ;
 . 8  int T, n-, m , P [MAXN] [MAXN], DP [MAXM];
 . 9  // time of day t i buy items remaining capacity is the maximum profit of $ j. 
10  int main ()
 . 11  {
 12 is      CIN T >> >> >> n- m;
 13 is      for ( int I = . 1 ; I <= T; I ++){
14         for(int j=1;j<=n;j++) {
15             scanf("%d",&p[i][j]);
16         }
17     }
18     for(int t=1;t<=T;t++){
19         memset(dp,0,sizeof(dp));
20         for(int i=1;i<=n;i++){
21             for(int j=p[t][i];j<=m;j++){
22                 dp[j]=max(dp[j],dp[j-p[t][i]]+p[t+1][i]-p[t][i]);
23             }
24         }
25         m=max(m,dp[m]+m);
26     }
27     cout<<m;
28     return 0;
29 }

// CSP2019 popular group t3

Guess you like

Origin www.cnblogs.com/yinyuqin/p/11980469.html