Monotone beacon transmission queue optimization DP--

Flames transfer

Beacon is an important military defense facilities, generally built in roads or advantageous place.

Once military occurs, daytime smoke, fire has passed military night.

In between the two cities have a n beacon towers, each beacon signals have a price.

In order to make accurate transmission of information, it must be sent at least a signal of m consecutive beacon.

Now the cost of input n, m and each beacon, calculate the exact cost of the total cost required to transfer information between a minimum of two cities much.

The input format
of the first two lines are integers n, m, to see the title described specific meaning;

The second row of n represents an integer in the cost of each beacon ai.

Output format
outputs only an integer representing the minimum cost.

data range
1 n , m 2 × 1 0 5 1≤n,m≤2×10^5 ,
0 a i 1000 0≤a_i≤1000
Input Sample:
. 5. 3
. 1. 5. 6 2 2
Output Sample:
4

answer:

We can read the title of this question is to know a sliding range of most value problem. We assume that f [i] for the i-th has been selected before the i-th Beacon cost and is not selected. So obviously the state transition equation f [i] = f [HOL queue] + w [i]. Because we monotonous queue maintenance is minimum. So we head team must be minimal.

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7;
int a[N],q[N],f[N];
int main()
{
    int n,m; cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    int hh=0,tt=0;
    for(int i=1;i<=n;i++){
        if(hh<=tt&&q[hh]<i-m) hh++;
        f[i]=f[q[hh]]+a[i];
        while(hh<=tt&&f[q[tt]]>=f[i]) tt--;
        q[++tt]=i;
    }
    int ans=0x3f3f3f3f;
    for(int i=n-m+1;i<=n;i++) ans=min(ans,f[i]);
    cout<<ans<<endl;
}

Published 92 original articles · won praise 6 · views 1131

Guess you like

Origin blog.csdn.net/weixin_42979819/article/details/104015664