Link: Https://www.nowcoder.com/acm/contest/96/H
Source: Niu Ke Net
Topic Description Today QWB to take part in a maths exam, this set of papers have n problems, each problem QWB can get score for AI,QWB not intend to finish all these questions,
He wanted to choose a total of 2k questions and expected him to get as big a score as possible, and he was going to choose 2 intervals of discontinuous length k,
That is, [l,l+1,l+2,...., l+k-1],[r,r+1,r+2,..., r+k-1] (R >= l+k).
Enter a description:
The first line is an integer t (t<=10) that represents the next line of two integers in the T-group data
n,k, (1<=n<=200,000), (1<=k,2k <= N) The
next line of n integers a1,a2,..., an , ( -100,000<=ai<=100,000)
Output Description:
Output an integer, the maximum score that QWB can get
A: dp1[i],dp2[i+1] saves the maximum value that can be obtained from the left and right two segments after the I cut.
After DP1,DP2, the enumeration tangent point can be derived from the recursive formula.
Code: #include <bits/stdc++.h> using namespace std;
#define LL Long const int maxn=200005;
ll A[MAXN],SUM1[MAXN],SUM2[MAXN],DP1[MAXN],DP2[MAXN];
int main () {int t;scanf ("%d", &t);
while (t--) {int n,k;scanf ("%d%d", &n,&k);
for (int i=0;i<=n+1;i++) Dp1[i]=dp2[i]=-1e18;
sum1[0]=sum2[n+1]=0;
for (int i=1;i<=n;i++) {scanf ("%lld", &a[i]);
Sum1[i]=sum1[i-1]+a[i];
if (i>k) sum1[i]-=a[i-k];
if (i==k) dp1[i]=sum1[i];
else Dp1[i]=max (sum1[i],dp1[i-1]);
for (int i=n;i>=1;i--) {sum2[i]=sum2[i+1]+a[i];
if (n-i>=k) sum2[i]-=a[i+k];
if (n-i<k) dp2[i]=sum2[i];
else Dp2[i]=max (Dp2[i+1],sum2[i]);
ll Ma=-1e18;
for (int i=k;i+k<=n;i++) Ma=max (DP1[I]+DP2[I+1],MA); Printf("%lld\n", MA);
return 0;
}