AcWing 1089. 烽火传递(单调队列优化dp)

2021-03-04 10:20:41 浏览数 (1)

思路:

代码:

代码语言:javascript复制
#include<bits/stdc  .h>
using namespace std;
#define int long long
const int N=2e5 10;
int n,k,a[N],f[N];
signed main(){
    cin>>n>>k;
    for(int i=1;i<=n;i  ){
        cin>>a[i];
    }
    deque<int>q;
    q.push_front(0);

    for(int i=1;i<=n;i  ){
        if(!q.empty()&&i-q.front()>k)q.pop_front();
        f[i]=f[q.front()] a[i];
        while(!q.empty()&&f[q.back()]>=f[i])q.pop_back();        
        q.push_back(i);
    }
    int ans=INT_MAX;
    for(int i=n-k 1;i<=n;i  )ans=min(ans,f[i]);
    cout<<ans<<endl;
}
dp

0 人点赞