The optimization of monotone queue in dynamic programming

Source: Internet
Author: User
Tags min stdin

Recently, the topic of monotonic queue, slope optimization has often appeared. Seeing the great gods around me is going to do it, and I can only keep up with them.
To take it slow, learn the monotony queue first. what type of DP is required to use the regular monotone queue.

A transfer equation like this can be used in a monotone queue:
F[i]=max (G[j]) +w[i] F[i]=max (G[j]) +w[i]
Where G[j] is a number that has nothing to do with I. W[i] is only related to I.

how to use.

We first open a queue. DP Time:
1, first delete the front out of range of the team head.
2, the use of Team head transfer.
3, the number and the end of the team, if the team tail is not better than it, the deletion of the tail, until the queue is empty or the team tail than it excellent. Finally add it to the end of the team. cause

1, the number in the monotone queue is within the range.
2, the team head of the best (otherwise early deleted by the back).
3, why not just save the team head. Because the team head is older, if out of range will be deleted. example Jzoj 1771. "Noip Dynamic Planning Theme" Beacon Transmission

Description
Beacon, also known as Beacon, is an important military defense facilities, generally built in the critical or traffic arteries. Once the enemy occurs, the daytime burns firewood, the smoke expresses the information, the night burns the dry wood, transmits the military intelligence through the fire, in the two cities has the N Beacon Tower, each beacon sends the signal to have a certain price. In order for the intelligence to be transmitted accurately, at least one signal must be signaled in a continuous M beacon. Please calculate the minimum cost of a total, so that the intelligence can be accurately transmitted between the two cities when the enemy strikes.
Input
First line: two integer n,m. where n denotes the number of beacons, m indicates that at least one signal must be signaled in a continuous M beacon. Next n rows, a number of WI per line, indicating the cost of signaling the first beacon.
Output
A line that represents the answer.
Sample Input
5 3
1
2
5
6
2

Sample Output
4

Data Constraint
For 50% of data, m≤n≤1,000. For 100% of data, m≤n≤100,000,wi≤100.

In the monotone queue, this is a water problem.
Set F[i] Indicates that I must select the minimum cost.
Initial:
F[0]=0 f[1..n]=∞
Equation:
F[i]=min (F[j]) +w[i]
and Max (0,I-M) <=j<i
Why J has such a range. If J can be smaller, then there will be j~i in this interval, it would be wrong. No gaps should be guaranteed.
Finally, the answer is to take the minimum value in F[N-M+1..N]
Time complexity O (nm) code without a monotonic queue

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define I_O (x) freopen ("#x". In "," R ", stdin); Freopen (" "#x". Out "," w ", stdout)
int n,m;
int w[100001];
int f[100001];
int main ()
{
    i_o (beacon);
    scanf ("%d%d", &n,&m);
    int i,j;
    for (i=1;i<=n;++i)
        scanf ("%d", &w[i]);
    Memset (f,127,sizeof f);
    f[0]=0;
    for (I=1;i<m;++i)
    {for
        (J=0;J<I;++J)//The reason for the two-paragraph write is for the card-you can also be together, and then the initial value of J is Max (0,i-m)
            f[i]=min (f[ I],F[J]);
        F[i]+=w[i];
    }
    for (I=m;i<=n;++i)
    {for
        (j=i-m;j<i;++j)
            f[i]=min (F[i],f[j]);
        F[i]+=w[i];
    }
    int ans=0x7f7f7f7f;
    for (i=n-m+1;i<=n;++i)
        ans=min (Ans,f[i]);
    printf ("%d\n", ans);
}

We find that this equation can be implemented in a monotone queue, and the optimal decision point can be saved with a monotone queue, and the time complexity is reduced from O (nm) to O (n). Positive Solution Code

 #include <cstdio> #include <cstring> #include <algorithm> using namespace
Std
#define I_O (x) freopen ("#x". In "," R ", stdin); Freopen (" "#x". Out "," w ", stdout) int n,m;
int w[100001];
int que[100001],head=0,tail=0;
int f[100001];
    int main () {i_o (beacon);
    scanf ("%d%d", &n,&m);
    int i,j;
    for (I=1;i<=n;++i) scanf ("%d", &w[i]);
    Memset (f,127,sizeof f);
    f[0]=0;
    que[0]=0; for (I=1;i<=n;++i) {if (que[head]<i-m) ++head;//Delete the out-of-range team header f[i]=f[que[head]]+w[i];/ /Transfer (with Team head) while (Head<=tail && f[que[tail]]>f[i])--tail;//will be no better than it all deleted que[++tail]
    =i;//add it to the end of the queue} int ans=0x7f7f7f7f;
    for (I=n-m+1;i<=n;++i) ans=min (Ans,f[i]);
printf ("%d\n", ans); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.