1 solutions

  • 1
    @ 2024-11-26 22:32:14

    十年oi一场空,不开longlong见祖宗 求和问题,和很有可能爆int,所以要longlong

    区间和,想到双指针,快速求区间和想到前缀和,于是便出现了华丽的解法:双指针+前缀和

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+9;
    using ll=long long;
    ll a[N],p[N];
    
    int main()
    { 
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        
        int n;cin>>n;
        for(int i=1;i<=n;++i)cin>>a[i];
        for(int i=1;i<=n;++i)p[i]=p[i-1]+a[i];//前缀和
        int k;cin>>k;//区间大小
        int i=0,j=1;//慢指针,快指针
        ll max_val=a[1];
        while(i!=j && j<=n)
        {
            if(j-i==k)
            {
                max_val=max(max_val,p[j]-p[i]);
                i++;
            }
            else
            {
                j++;
            }
        }
        cout<<max_val;
        return 0;
    }
    
    
    • 1

    Information

    ID
    4916
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    10
    Accepted
    3
    Uploaded By