单调队列模板

Description

有N个数(N<=100000) ,在连续M(M<=N)个数里至少要有一个数被选择. 
求选出来数的最小总和。 

Input

第一行两个整数 N,M 
接下来N行 Wi(Wi<=100) 表示第i个数 

Output

一个整数,最小总和 

Sample Input

5 3 
1 
2 
5 
6 
2 

Sample Output

4

模板题,大家学习一下手撸单调队列,不要太依赖STL,大多数时候,很多人就是被STL坑成TLE

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,m,l=1,r=1,ans=9999999,f[100001],a[100001],t[100001];
 4 int main()
 5 {
 6     ios::sync_with_stdio(false);
 7     cin>>n>>m;
 8     for(int i=1;i<=n;i++)
 9         cin>>a[i];
10     for(int i=1;i<=n;i++)
11     {
12         while(l<=r&&i-t[l]>m)l++;
13         f[i]=f[t[l]]+a[i];
14         while(l<=r&&f[t[r]]>f[i])r--;
15         t[++r]=i;
16     }   
17     for(int i=n-m+1;i<=n;i++)
18         ans=min(ans,f[i]);
19     cout<<ans;     
原文地址:https://www.cnblogs.com/lcxer/p/9441686.html