F (i) =min{f (j) + (D (j) <=d (i))} (Max (1,i-k) <=j<=i)
There are two variables that are difficult to use in a monotone queue, but (reference):
IfFI<fJ ,IMust be better thanJExcellent. Because ifhEighti≥heightJ Needless to say, ifhEightI<heightJ , i can jump to high places at a cost of 1 , like J , no worse than J .
If fi =fj , which of course is who's Hei gh t High who is excellent.
< Span class= "Mathjax" > double key words just make it right.
#include <cstdio>using namespace std; #define N 1000001struct point{int x, y;}; BOOL operator <= (const point &a,const point &b) {return a.x!=b.x?a.x<b.x:a.y>=b.y;} int n,m,k,q[n]; Point Dp[n];int Main () {scanf ("%d", &n), for (int i=1;i<=n;++i) scanf ("%d", &dp[i].y), scanf ("%d", &m); (; m;--m) { scanf ("%d", &k); int head=1,tail=1; dp[1].x=0; Q[1]=1; for (int i=2;i<=k;++i) { dp[i].x=dp[q[head]].x+ (DP[Q[HEAD]].Y<=DP[I].Y); while (Dp[i]<=dp[q[tail]] && tail>=head)--tail; q[++tail]=i; } for (int i=k+1;i<=n;++i) { dp[i].x=dp[q[head]].x+ (DP[Q[HEAD]].Y<=DP[I].Y); if (q[head]<i-k+1) ++head; while (Dp[i]<=dp[q[tail]] && tail>=head)--tail; q[++tail]=i; } printf ("%d\n", dp[n].x); } return 0;}
"Monotone queue" "Dynamic planning" bzoj3831 [Poi2014]little Bird