愿你归来
BurnDawn1012 · · 题解
CF2028C
前言
鉴定为思维题。二分显然可做,但是双指针显然
爱丽丝可爱!以此篇送可可爱爱的爱丽丝 @ARIS1_0 远走。
思路
爱丽丝最喜欢的算法是双指针。
所以,不难发现:如果区间
爱丽丝常常苦思冥想。
所以,不难发现:有一个不好处理的地方,就是如何确定
爱丽丝最喜欢的 trick 是前缀和。
所以,不难发现:用
AC Code
:::success[code]
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int T, n, m;
long long a[N], v;
long long pre[N], lst[N];
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> T;
while (T -- ){
memset(pre, 0, sizeof pre);
memset(lst, 0, sizeof lst);
cin >> n >> m >> v;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
long long sum = 0;
for (int i = 1; i <= n; i ++ ){
sum += a[i];
if (sum >= v) pre[i] = pre[i - 1] + 1, sum = 0;
else pre[i] = pre[i - 1];
}
sum = 0;
for (int i = n; i >= 1; i -- ){
sum += a[i];
if (sum >= v) lst[i] = lst[i + 1] + 1, sum = 0;
else lst[i] = lst[i + 1];
}
if (pre[n] < m){
cout << "-1\n";
continue;
}
sum = 0;
long long ans = -1;
for (int l = 1, r = 0; l <= n; l ++ ){
sum -= a[l - 1];
while (r < n && lst[r + 2] + pre[l - 1] >= m) sum += a[++ r];
ans = max(ans, sum);
}
cout << ans << "\n";
}
}
:::
后记
愿你吃到最多的蛋糕。
致我永远的好朋友 @ARIS1_0。
祝好。