样例没过的菜鸡求救
查看原帖
样例没过的菜鸡求救
86971
TRZ_2007楼主2021/4/30 15:16

Rt,用了滚动数组,样例二就挂掉了,然而貌似并没有出现转移方程的锅。

#include <cstdio>
#include <cctype>
using namespace std;

#define gc getchar
inline int read() {
	int c = gc(), t = 1, n = 0;
	while(isspace(c)) { c = gc(); }
	if(c == '-') { t = -1, c = gc(); }
	while(isdigit(c)) { n = n * 10 + c - '0', c = gc(); }
	return n * t;
}
#undef gc

const int N = 5e3 + 9;
typedef long long ll;
const ll inf = 2007040820070408;

ll f[2][N],ans = -inf,a[N];
int n,w,s;

inline int min(int a,int b) { return a > b ? b : a; }
inline ll max(ll a,ll b) { return a > b ? a : b; }

int main() {
	n = read(); w = read(); s = read();
	for(int i = 1;i <= n;i++) a[i] = read();
	for(int i = 0;i <= w;i++) f[0][i] = f[1][i] = -inf;
	f[0][0] = 0;
	for(int i = 1;i <= n;i++) {
		for(int j = 1;j <= w;j++) {
			for(int k = j - 1;k <= min(w,j + s - 1);k++) {
				f[i % 2][j] = max(f[i % 2][j],f[(i - 1) % 2][k] + j * a[i]);
			}
		}
	}
	for(int i = 1;i <= w;i++) ans = max(ans,f[n % 2][i]);
	printf("%lld\n",ans);
	return 0;
}
/*
5 3 3
1 -3 -2 4 5
21*/
2021/4/30 15:16
加载中...