解题思路:
先求出前缀和,然后求出遍历求出坐标i后长度为k的区间的最大值,最后求出最大的pre[i+k-1]-pre[i-1]+m[i],时间复杂度为O(N*T)
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+10;
int t,n,k;
ll pre[maxn],m[maxn];
int main()
{
cin>>t;
while(t--)
{
cin>>n>>k;
for(int i=1;i<=n;++i)
{
int temp;
cin>>temp;
pre[i]=pre[i-1]+temp;
}
ll ans=-0x3f3f3f3f3f3f3f3f;
m[n-k+2]=ans;
for(int i=n-k+1;i>=1;--i)
{
ll temp=pre[i+k-1]-pre[i-1];
if(temp>m[i+1]) m[i]=temp;
else m[i]=m[i+1];
}
for(int i=1;i+2*k-1<=n;++i)
{
ans=max(ans,pre[i+k-1]-pre[i-1]+m[i+k]);
}
cout<<ans<<endl;
}
return 0;
}