第一届ACC C.选取数对
Question:AcWing 4378.选取数对
Question Link:acwing.com/problem/content/4381
Algorithm:DP
Question Analysis:
闫氏dp分析法:
Code:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5010;
typedef long long LL;
LL s[N],f[N][N];
int n,m,k;
int main()
{
cin >> n >> m >> k;
for (int i = 1; i <= n; i ++ )
{
int x;
cin >> x;
s[i]=s[i-1]+x;
}
for (int i = 0; i <= n; i ++ ) f[i][0]=0;//如果一个数对都不选,sum值为0
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= k; j ++ )
{
if(i-m<0) f[i][j]=f[i-1][j];
else f[i][j]=max(f[i-m][j-1]+s[i]-s[i-m],f[i-1][j]);
}
cout << f[n][k] << endl;
}
感谢AcWing中许多大佬的题解 Orz