P8775 [蓝桥杯 2022 省 A] 青蛙过河

原题链接

题解

一只青蛙 \(x\) 天来回跳 $\to $ 一只青蛙从左往右跳 \(2x\)\(\to\) \(2x\) 只青蛙从左往右跳一次
规律:对于任意长度为 \(y\) 的区间,其区间和一定不小于 \(2x\)
证明过程请看题解区,非常优雅

upd:
如果想从起点跳到石头上,那么前y个石头的总和一定大于2x,后y个同理
由此想到每y个石头的和都大于2x

code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,x;
    cin>>n>>x;
    int pres[n+5]={0};
    for(int i=1;i<n;i++)
    {
        int a;
        cin>>a;
        pres[i]=pres[i-1]+a;
    }
    int l=1,ans=0;
    for(int r=1;r<n;r++)
    {
        while(pres[r]-pres[l-1]>=2*x&&l<=r)l++;
        ans=max(ans,r-l+2);
    }
    cout<<ans<<endl;
    return 0;
}

posted @ 2024-02-21 01:44  纯粹的  阅读(123)  评论(0)    收藏  举报