Luogu2938 [USACO09FEB]股票市场Stock Market (DP,多重背包)

第n天不卖,视为卖了又原价买回

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("

----------

")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 53;

int f[500003];
int price[53][11];
int sum[53][11];
int main(){
//FileOpen();

	int n, D, m;
	io >> n >> D >> m;
	
	R(i,1,n){
		R(j,1,D){
			io >> price[i][j];
			sum[i][j] = price[i][j] - price[i][j - 1];
		}
	}
	
	R(i,2,D){
		Fill(f, 0);
		int maxx = 0;
		R(j,1,n){
			R(k,price[j][i - 1],m){
				f[k] = Max(f[k], f[k - price[j][i - 1]] + sum[j][i]),
				maxx = Max(maxx, f[k]);
			}	
		}
		m += maxx;
	}
	
	printf("%d", m);
	
	return 0;
}

原文地址:https://www.cnblogs.com/bingoyes/p/11259237.html