CF762D Maximum Path
题目戳这里。
首先明确一点,数字最多往左走一次,走两次肯定是不可能的(因为只有\(3\)行)。
然后我们用\(f_{i,j}\)表示前\(i\)行,第\(i\)行状态为\(j\)的最优解。(\(j\)表示从第一,二,三,行出来,或者是朝左走了)。
方程应该也好YY。
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int maxn = 100010; const ll inf = 1LL<<60;
int N; ll f[maxn][4],A[4][maxn];
inline int gi()
{
char ch; int ret = 0,f = 1;
do ch = getchar(); while (!(ch >= '0'&&ch <= '9')&&ch != '-');
if (ch == '-') f = -1,ch = getchar();
do ret = ret*10+ch-'0',ch = getchar(); while (ch >= '0'&&ch <= '9');
return ret*f;
}
int main()
{
freopen("762D.in","r",stdin);
freopen("762D.out","w",stdout);
N = gi();
for (int i = 1;i <= 3;++i) for (int j = 1;j <= N;++j) A[i][j] = gi()+A[i-1][j];
for (int i = 0;i <= N;++i) for (int j = 0;j < 4;++j) f[i][j] = -inf;
f[0][1] = 0;
for (int i = 1;i <= N;++i)
{
for (int j = 1;j <= 3;++j)
for (int k = 1;k <= 3;++k) f[i][j] = max(f[i-1][k]+A[max(j,k)][i]-A[min(j,k)-1][i],f[i][j]);
f[i][1] = max(f[i][1],f[i-1][0]+A[3][i]);
f[i][3] = max(f[i][3],f[i-1][0]+A[3][i]);
f[i][0] = max(f[i][0],max(f[i-1][1],f[i-1][3])+A[3][i]);
}
cout << f[N][3] << endl;
fclose(stdin); fclose(stdout);
return 0;
}
高考结束,重新回归。