[BZOJ3831] [POI2014] Little Bird monotonous queue, ''no more brush it on the soil'' series ., Bzoj3831poi2014
Question:
I am sticking to the Black translation of bzoj ~
There are n trees in a row. The height of the I-th tree is Di. MHY wants to go from the first tree to the nth tree to find his sister. If MHY is in the I tree, he can jump to the I + 1, I + 2,..., I + k tree. If MHY jumps to a tree that is not less than the current tree, the fatigue value will be + 1; otherwise, it will not. In order to have the power to play with the girl, MHY should minimize the fatigue value. Question:
Monotonous queue breaks the question, no more.
It is said that the bzoj poi has always been a local tyrant, and it is not enough to brush it ..
Let's look at the code again. There are comments in it.
Code:
# Include <cstdio >#include <cstring> # include <iostream> # include <algorithm> # define N 1001000 # define inf 0x3f3f3fusing namespace std; int q [N], top, tail, n; int f [N], high [N]; bool cmp (int ha, int fa, int hb, int fb) // returns whether A is better than B {return fa = fb? Ha> = hb: fa <fb; // I. The same price must be high. // II. Different prices must be cheap. // This is because: // ① the price is high: no explanation. // ② The two are the same height: do not explain. // ③ Cheap dwarf: it can change from 1 to infinitely high. // or it can be transferred back to the dwarf tree .} Int main () {// freopen ("test. in "," r ", stdin); int I, j, k, g; scanf (" % d ", & n); for (I = 1; I <= n; I ++) scanf ("% d", & high [I]); for (scanf ("% d", & g); g --;) {scanf ("% d", & k); q [top = tail = 1] = 1; for (I = 2; I <= n; I ++) {while (top <tail & I-q [top]> k) top ++; f [I] = f [q [top] + (high [I]> = high [q [top]); while (top <= tail & cmp (high [I], f [I], high [q [tail], f [q [tail]) tail --; q [++ tail] = I;} printf ("% d \ n", f [n]);} return 0 ;}