TLE on 9~12 玄关

P2627 [USACO11OPEN] Mowing the Lawn G

c_y_z @ 2024-10-12 09:09:19

#include<bits/stdc++.h>
#define ll long long
#define rint register int
using namespace std;
const int N=1e5+10; 
int n,k,a[N];
ll sum[N],f[N][2];
int q[N];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0); 
    cin>>n>>k;
    for(rint i=1;i<=n;i++) cin>>a[i];
    for(rint i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
    for(rint i=1;i<=n;i++){
        f[i][0]=max(f[i-1][0],f[i-1][1]);
        int l=1,r=0,j=0;
        for(;j<i;j++){
            while(l<=r&&f[q[r]][0]-sum[q[r]]<=f[j][0]-sum[j]) r--;
            q[++r]=j;
        }
        while(l<=r&&q[l]<i-k) l++;
        f[i][1]=f[q[l]][0]+sum[i]-sum[q[l]];
    }
    cout<<max(f[n][1],f[n][0]);
    return 0;
}

by gghack_Nythix @ 2024-10-12 09:17:16

@c_y_z 啊,但是你的单调队列貌似是 \mathcal{O(n^2)} 的,注意 j 的循环变化、


by K_J_M @ 2024-10-12 09:25:39

@c_y_z 但我暴力拿了90pts


by c_y_z @ 2024-10-12 09:25:57

蟹蟹,我显然唐了


by c_y_z @ 2024-10-12 09:31:10

@K_J_M me to


|