B. LECTURE SLEEP

http://www.yyycode.cn/index.php/2020/05/20/b-lecture-sleep/


Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells a i theorems during the i-th minute.

Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka’s behavior. If Mishka is asleep during the i-th minute of the lecture then t i will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told — a i during the i-th minute. Otherwise he writes nothing.

You know some secret technique to keep Mishka awake for k minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and n - k + 1. If you use it on some minute i then Mishka will be awake during minutes j such that  and will write down all the theorems lecturer tells.

You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.Input

The first line of the input contains two integer numbers n and k (1 ≤ k ≤ n ≤ 105) — the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.

The second line of the input contains n integer numbers a 1, a 2, … a n (1 ≤ a i ≤ 104) — the number of theorems lecturer tells during the i-th minute.

The third line of the input contains n integer numbers t 1, t 2, … t n (0 ≤ t i ≤ 1) — type of Mishka’s behavior at the i-th minute of the lecture.Output

Print only one integer — the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.ExampleinputCopy

6 3
1 3 5 2 5 4
1 1 0 1 0 0

outputCopy

16

Note

In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16.


题意:每个点老师会讲一点量的知识点,张三会在1的时候醒着在0的时候睡觉,你现在能在1-n-k+1的时间点内给张三喝咖啡让他持续醒着k分钟,问最多能听到多少知识点

思路:刚开始的时候用双指针发现不好写,因为这是个固定区间长度往前走的问题。实际上用一个指针的for循环去做。但是我刚刚开始的思路是卡了边界的。

附上WA5代码:

这个代码的问题在于 1 2 3 4 5 6

1 1 1 1 0 0 这样的是加不上后面的

所以说,我太菜了。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2e5+10;
typedef long long LL;
LL a[maxn];
LL type[maxn];
LL sum[maxn];
int main(void)
{
	LL n,k;cin>>n>>k;
	LL pos=1;LL flag=1;
	for(LL i=1;i<=n;i++) cin>>a[i];
	for(LL i=1;i<=n;i++) 
	{
		cin>>type[i];
		if(type[i]==0&&flag==1) pos=i,flag=0;///记录第一个睡觉点的 
	}
	LL ans=0;
	for(LL i=1;i<=n;i++) if(type[i]) ans+=a[i];
	for(LL i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
	LL res=0;
	LL j=pos;LL r=pos+k-1;
	for(LL i=pos;i<=n; )
	{
			if(res<=sum[i+k-1]-sum[i-1]) res=sum[i+k-1]-sum[i-1],j=i,r=i+k-1;
			i++;
	}
	res=max(res,sum[min(r,n)]-sum[j-1]);
	LL sub=0;
	for(LL i=j;i<=r;i++) if(type[i]) sub+=a[i];

	cout<<(ans+res-sub)<<endl;
return 0;
}

那只能转变思路,用多个前缀和统计,在固定区间长度往前走的时候统计整段的全部的知识点取最大

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2e5+10;
typedef long long LL;
LL a[maxn];
LL type[maxn];
LL sum[maxn];
LL per[maxn];
int main(void)
{
	LL n,k;cin>>n>>k;
	for(LL i=1;i<=n;i++) cin>>a[i],sum[i]=sum[i-1]+a[i];//总前缀和 

	for(LL i=1;i<=n;i++) 
	{
		cin>>type[i];//类型 
		if(type[i]) per[i]=per[i-1]+a[i];///这个点没睡觉的前缀和 
		else per[i]=per[i-1];///这个点睡觉的前缀和 
	}
 	LL res=0;
	for(LL i=1;i<=n-k+1;i++)
	{	
		LL x=per[i-1];//这个时间点之前和 (睡了的没听) 
		LL y=sum[i+k-1]-sum[i-1];///这个点到清醒时间的和 (肯定在学习知识) 
		LL z=per[n]-per[i+k-1];//清醒时间到下课的和 (以后到下课前就看自己的造化)
		res=max(res,x+y+z);///算出最长的一整段 
	}
	cout<<res<<endl;
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/106238935