「杂题乱刷」CF961B

题目链接

算法一:

直接暴力,时间复杂度 O(n2)

算法二:

使用双指针维护,时间复杂度 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 @   wangmarui  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示