Max Sum Plus Plus
Topic meaning:
- Give you n numbers, let you find m consecutive and disjoint intervals from them, and then make the sum of all interval elements to be the largest.
data range:
。
Problem solving ideas:
First determine a state, dp[i][j] represents the maximum value of group i formed by the first j numbers, and the state transition equation is:
AC code:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL INF = 1LL << 50;
const int maxn = 1e6;
LL Max[maxn + 5];
LL dp[maxn + 5];
LL a[maxn + 5];
LL Maxm;
int n, m;
int main() {
while(scanf("%d%d", &m, &n) != EOF) {
for(int i = 1; i <= n; i++)scanf("%lld", &a[i]);
memset(dp, 0, sizeof(dp));
memset(Max, 0, sizeof(Max));
for(int i = 1; i <= m; i++) {
Maxm = -INF;//Maxm每次初始化为-oo
for(int j = i; j <= n; j++) {
dp[j] = max(dp[j - 1], Max[j]) + a[j];
Max[j] = Maxm;//Max[j]记录上一状态前j-1个的最大值。
Maxm = max(Maxm, dp[j]);
}
}
printf("%lld\n", Maxm);
}
return 0;
}