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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章