P5662 Souvenirs

P5662 Souvenirs

answer

I expect to get the title DP, but just do not know how to write

It turns out this is a backpack DP (recently put together a backpack white whole 

 

We observed that the purpose of special about this question:

That is, for the hands of the items we can buy today and sell tomorrow morning then, of course, if you want to have to hold items, then you can also then buy them back tomorrow, so we do not consider the time of day decision-making hand carried items, because all the money in your hand, and on the first day that the situation is not similar thing

Then every day is a new beginning day similar to the former, then we can still deal with in accordance with the previous day's practice today, and today is that you have n items, each item can buy more, then consider them tomorrow morning all sold up to how much money you can get, and then use the money to sell tomorrow morning as the new capital to tomorrow's decision-making

So we can expect full backpack

dp [i] [j] [ k] to the first day i, j-th items left in the hands of k dollars, tomorrow morning all the items sold in their hands can get the maximum profit

dp [ i ][ j ][ k ] = max ( dp [ i ][ j ][ k ] , dp [ i ][ j-1 ][ k - p[ i ][ j ] + p[ i+1 ][ j ] - p[ i ][ j ] )

Then a dimensional reduction  dp [k]

dp [ k ] = max ( dp [ k ] , dp [ k - p[ i ][ j ] ] + p[ i+1 ][ j ] - p[ i ][ j ]  )

We choose the maximum profit today, plus today's original capital (of course, if the whole today is certainly a loss of capital is to select directly under the original capital as the day begins), as the next day began the capital

 

Note dp array setting is profit, so every time you start the day with an array must be cleared dp

 

Code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<queue>

using namespace std;

typedef long long ll;

inline int read()
{
    int ans=0;
    char last=' ',ch=getchar();
    while(ch<'0'||ch>'9') last=ch,ch=getchar();
    while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    if(last=='-') ans=-ans;
    return ans;
}

int t,n,m,ans=0;
int p[105][105];
int dp[10003];

int main()
{
    t=read();n=read();m=read();
    for(int i=1;i<=t;i++)
        for(int j=1;j<=n;j++)
           p[i][j]=read();
    ans=m;
    for(int i=1;i<=t;i++){
        memset(dp,0,sizeof(dp));
        for(int j=1;j<=n;j++)
            for(int k=p[i][j];k<=m;k++){
                dp[k]=max(dp[k],dp[k-p[i][j]]+p[i+1][j]-p[i][j]);
            }
        for(int j=0;j<=m;j++) ans=max(ans,dp[j]+m);
        m=ans;
    }
    printf("%d\n",m);
    return 0;
}

Guess you like

Origin www.cnblogs.com/xiaoyezi-wink/p/12041122.html