贝壳找房2021笔试题
解析:
将总的和看做是两部分:已经为1的和与处理后为1的和,第一部分是固定值,所以本题就转化成了求第二部分的最大值
滑动窗口的思想
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 100010;
int n, m;
int a[N], b[N];
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < n; i++) scanf("%d", &b[i]);
LL sum = 0;
for (int i = 0; i < n; i++)
if (b[i]) sum += a[i];
LL v = 0, s = 0;
for (int i = 0; i < n; i++) {
if (!b[i]) s += a[i];
if (i >= m && !b[i - m]) s -= a[i - m];
v = max(v, s);
}
printf("%lld\n", sum + v);
return 0;
}