尺取法模板
注意 每次取出的钱不能超过ATM机剩余的余额:
sum+a[t]+S>=0sum + a[t] + S >= 0sum+a[t]+S>=0
typedef long long ll;
const int N = 2e5+10;
int n;
ll S, a[N];
void solve()
{
scanf("%d %lld", &n, &S);
for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
int res = 0;
ll sum = 0;
int s = 1, t = 1, l = 0, r = 0;
while(1)
{
while(t <= n && sum + a[t] + S >= 0) sum += a[t++];
//if(sum + S < 0) break; //
if(t - s > res) {
res = t - s;
l = s;
r = t - 1;
}
if(t >= n && s >= n) break;
sum -= a[s++];
}
if(!res) printf("-1\n");
else printf("%d %d\n",l,r);
}
这篇博客讲解了一种解决ATM取款问题的算法,使用尺取法模板,确保每次取款不超过余额,并通过代码展示了如何在给定余额限制下找到最优取款组合。

350

被折叠的 条评论
为什么被折叠?



