其他分享
首页 > 其他分享> > 烽火传递

烽火传递

作者:互联网

#include<cstdio>
#include<deque>
#define MAXN 1000005

inline int min(int x,int y){return x<y?x:y;}

int n,m,ans=1e9,a[MAXN],dp[MAXN];

std::deque<int> q;

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    q.push_back(0);
    for(int i=1;i<=n;i++){
        while(!q.empty()&&q.front()<i-m)q.pop_front();
        int u=q.front();
        dp[i]=dp[u]+a[i];
        while(!q.empty()&&dp[q.back()]>dp[i])q.pop_back();
        q.push_back(i);
    }
    for(int i=n-m+1;i<=n;i++)ans=min(ans,dp[i]);
    printf("%d\n",ans);
}

<=i-m调了两年

标签:main,return,min,int,back,传递,include,烽火
来源: https://www.cnblogs.com/Y15BeTa/p/11780693.html