Monotonic queue optimization DP
topic
The beacon tower is an important military defense facility, and is generally built on traffic arteries or dangerous places.
Once a military situation occurs, heavy smoke will be used during the day and firelight at night to convey the military situation.
There are n beacon towers between a certain two cities, and each beacon tower sends a signal with a certain price.
In order to transmit information accurately, at least one of the m consecutive beacon towers must send out a signal.
Now enter n, m and the cost of each beacon tower. Please calculate the minimum total cost required to accurately transmit information between the two cities.
Input format The
first line is two integers n, m, see the title description for the specific meaning;
The n integers in the second line represent the cost ai of each beacon tower.
Output format The
output is only an integer, which represents the minimum cost.
Data range
1≤n,m≤2×105,
0≤ai≤1000
Input example:
5 3
1 2 5 6 2
Output example:
4
Ideas
Analysis icon:
state represents: f[i]
a front 1~i
seat beacon condition is satisfied, and the first i
beacon towers ignition scheme set.
Attribute: The smallest cost value in the set of all eligible solutions.
State calculation:
How to divide the collection?
In a continuous subject of the request m
to have a least one beacon signals, i.e. successive m
beacon towers must have at least one is ignited. The f[i]
meaning of the representation, the first i
seat has been ignited, and therefore at the i
front seat forward m
at least to have a lighted beacon towers. It can be the first i-m
, the first i-m+1
, the first i-3
, the first i-2
, the first , the first i -1
.
So the state calculation equation: f [ i] = min( f[j] ) + w[i]
(i-m<=j<=i-1)
The maximum value of an interval can be solved by a monotonic queue. In this question, we define a monotonically increasing queue, in which the maintenance of the f[j
] set is. Each time the head of the line is taken out, that is m
, the one with the smallest value in the length of the interval is f[j]
used to update the answer.
In fact, there is a small question , why should the first i
seat be expressed as lit when the state is expressed ?
We can start from the problem, every n
beacon tower must have one to be lit. So the n
same is true for the last beacon. If we f[i]
defined as a front 1~i
to meet the conditions beacon towers, and the first i
beacon towers lit collection program. Then the answer must be in f[n-m+1],f[n-m+2],,,,,,f[n]
between. In other words, the i
answer can be easily expressed by expressing the first seat as lit. This gives us an inspiration. When we define the state representation, we must consider whether the state we define can contain answers.
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=2e5+10;
int w[N];
int f[N];
int q[N];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&w[i]);
f[0]=0; //0座的代价为0
int tt=0,hh=0;
for(int i=1;i<=n;i++)
{
if(q[hh]<i-m) hh++; //超出滑动窗口,踢出队列
f[i]=f[q[hh]]+w[i]; //更新答案
while(hh<=tt&&f[q[tt]]>=f[i]) tt--;//维护单调递增队列 在一段区间内
//找出最小的f[j](i-m<=j<=i-1)
q[++tt]=i;
}
int res=1e9;
for(int i=n-m+1;i<=n;i++) res=min(res,f[i]); //答案在最后一段区间选
printf("%d\n",res);
return 0;
}