题意:有一个人很喜欢老师讲的微积分,但是再喜欢,也有时候要打瞌睡。输入n和k,n代表老师讲的n分钟,然后每分钟老师讲的公式。下面一行输入0和1对应上面的每分钟,如果为1,代表这个人听了老师课,并记下来了公式,如果为0,代表他睡着了,没有记下来公式。你有个技能,能够瞬间把他弄醒,让他继续听课,持续k分钟,,然后能记录下公式。让我们求出最大的记录公式数。
题解:先把所有能够听到的公式数求和,对没有听到的公式数进行前缀和 然后连续遍历k个数,求出最大值。就是你让他醒的时候能够听到的最大公式数。最后答案就是ans+maxn。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int a[N],b;
int main()
{
int n,k,ans=0,maxn=0;
cin>>n>>k;
for(int i=1; i<=n; i++)
cin>>a[i];
for(int i=1; i<=n; i++)
{
cin>>b;
if(b) ///先把所有这个上课能听的加起来
{
ans+=a[i];
a[i]=0; ///把已经加过的a[i]置为0
}
a[i]=a[i-1]+a[i];///打瞌睡的时候,没听到的,用前缀和保存
}
for(int i=k; i<=n; i++)///所有数中,遍历连续k个数字的最大和
maxn=max(maxn,a[i]-a[i-k]);
cout<<ans+maxn<<endl;
return 0;
}