1 条题解

  • 0
    @ 2025-5-12 20:34:49

    双指针(不能用负数数据)

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+5,INF=0x3f3f3f3f;
    typedef long long LL;
    int n,l=1,r=1,ans;
    LL m,a[N],sum;
    int main() {
        cin>>n>>m;
        for(int i=1;i<=n;i++)cin>>a[i];
        while(r<=n){
            sum+=a[r];
            while(sum>m){
                sum-=a[l];
                l++;
            }
            if(sum==m)ans++;
            r++;
        }
        cout<<ans<<endl;
        return 0;
    }
    
    

    信息

    ID
    3240
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    (无)
    递交数
    373
    已通过
    48
    上传者