「杂题乱刷」CF961B

题目链接

算法一:

直接暴力,时间复杂度 \(O(n^2)\)

算法二:

使用双指针维护,时间复杂度 \(O(n)\)

算法三:

是用前缀和维护,时间复杂度 \(O(n)\)

这里提供算法二的代码:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define cin(x) cin>>x
#define cout(x) cout<<x;
#define lowbit(x) x&-x
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
long long n,m,a[100010],b[100010],c[100010],r,sum,maxn;
int main()
{
	IOS;
	cin>>n>>m;
	forl(i,1,n)
		cin>>a[i];
	forl(i,1,n)
		cin>>b[i];
	r=m;
	forl(i,1,r)
		sum+=a[i];
	forl(i,r+1,n)
		if(b[i])
			sum+=a[i];
	forl(i,1,n-m+1)
	{
		maxn=max(maxn,sum);
		if(!b[i])
			sum-=a[i];
		if(!b[r+1])
			sum+=a[r+1];
		maxn=max(maxn,sum);
		r++;
	}
	cout<<maxn;
	QwQ;
}
posted @ 2023-12-16 00:42  wangmarui  阅读(4)  评论(0编辑  收藏  举报