P8226 「Wdoi-5」樱点收集 题解
题意:
一个人吃豆豆,沿路上有若干段豆豆,这个人挨着吃,吃
假设每一段都有豆豆。
首先考虑不能不吃的情况:
将每段豆豆数量做前缀和,统计有垃圾桶且前缀和模
再加上可以有一段不吃的情况:
枚举跳过的一段,如果后面某一个位置的前缀和减去这段豆豆数后模
由于
计算时若某一段无豆豆,则跳过,不用其前缀和更新。
时间复杂度:
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 300010, M = 1000010;
int n, m;
LL a[N], s[N], res, K;
int nl[M], nr[M];
bool st[N];
int main(){
scanf("%d%d%lld", &n, &m, &K);
for(int i = 1; i <= m; i ++){
int t;
scanf("%d", &t);
st[t] = true;
}
for(int i = 1; i <= n; i ++){
scanf("%lld", &a[i]);
s[i] = a[i] + s[i - 1];
if(a[i] && st[i]) nl[s[i] % K] ++;
}
int res = nl[0];
for(int i = n; i >= 1; i --){
if(a[i] && st[i]) nl[s[i] % K] --;
res = max(res, nl[0] + nr[a[i] % K]);
if(a[i] && st[i]) nr[s[i] % K] ++;
}
printf("%d\n", res);
return 0;
}