AcWing 1089. 烽火传递 deque实现
原题链接
中等
作者:
负壹
,
2020-06-22 16:28:29
,
所有人可见
,
阅读 702
C++ 代码
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int n,m;
int w[N],f[N];
int main(){
cin>>n>>m;
for(int i = 1;i<=n;i++)cin>>w[i];
deque<int>q;
q.push_back(0);
for(int i = 1;i<=n;i++){
if(q.size()&&q.front()<i-m)q.pop_front();
f[i] = f[q.front()]+w[i];
// cout<<i<<" "<<f[i]<<endl;
while(q.size()&&f[i]<=f[q.back()])q.pop_back();
q.push_back(i);
}
int ans = 1e9;
for(int i = n-m+1;i<=n;i++)
ans = min(ans,f[i]);
cout<<ans<<endl;
return 0;
}
定义一到全局变量为什么也不行啊
为什么要加q.push_back(0)
从1开始,就像数组模拟的hh = 0,tt = 0一样