& Monotone monotone queue queue optimization the DP & recommended topics

Look at a question

scanning

Description Title
has a 1 * n matrix, with n a positive integer.

Now you can cover a number of boards consecutive k.

A first number of the start board cover 1 ~ k matrix, each time a mobile unit board to the right, until a right end of the first n numbers coincide.

The maximum number of output before each move to be covered is.

Input format
of the first two line numbers, n, k, represents a total number of n, the number of boards may cover k.

Second line number n, the elements of the matrix.

Output Format
co n - k + 1 lines, each a positive integer.

Denotes the i-th row i ~ i + k - 1 in number is the maximum number.

Description / prompts
to 100% of the data to ensure that: 1 ≤ n ≤ 2 * 1e6,1 ≤ k ≤ n

The elements of the matrix size does not exceed 1e4.


This question is obviously no way we violence (extreme complexity O ( N 2 ) O (N ^ 2)

Then we find a way to optimize, this time on the need to introduce our monotonous queue

Monotonous queue

We need to maintain a queue to keep him monotonic (incremented or decremented)

It is the image of some guy handsome than anyone else, everyone has a height and color values

First came a xyc, his height is 1m, color values -\infty , but because the team was empty, so we let him into the team

Queue becomes

Numbering Full name height Yen value
1 XYC 1m -\infty

Later came a gjm, his height is 1.1m, Yen value is 0, then, xyc both shorter than gjm, and no gjm handsome, so xyc certainly not the "handsome than the General Assembly," the champion, so we put he kicked queue

Queue becomes

Numbering Full name height Yen value
2 Gjm 1.1m 0 0

Later came a ygl, ygl height 1.7m, color values 100 -100

Although ygl Yen value lower than gjm, but he is taller than gjm ah, elevation can also be embodied handsome, so gjm still have hope, we have to join the queue ygl

Numbering Full name height Yen value
2 Gjm 1.1m 0 0
3 ygl 1.7m 100 -100

Finally came a ljr, ljr nine meters tall, handsome degrees + +\infty

So he easily beating and hanging up [beating and hanging up (beating and hanging of the training team xyc) of gjm], he became the champion, gjm and ygl can only be kicked out because of their height and color values ​​are not high ljr

Numbering Full name height Yen value
4 ljr 1.9m + +\infty

After this case we know the monotony of the queue thinking, there are two parameters a i a_i with b i b_i If there is a j meet a i < a j a_i <a_j and b i < b j b_i<b_j , Then we put i to kick queue

So we go back to that question above

We maintain a monotonous queue, which is the maximum range, but we want to make the queue inside monotonically decreasing, so every time to get the maximum interval is the head of the team

Monotone queue by S T L STL is achieved, but usually need to use deque d e q u e and

Generally recommend you handwritten queue

P2032 Code

# include <cstdio>
# include <algorithm>
# include <cstring>
# include <cmath>
# include <climits>
# include <iostream>
# include <string>
# include <queue>
# include <vector>
# include <set>
# include <map>
# include <cstdlib>
# include <stack>
# include <ctime>
using namespace std;

# define Rep(i,a,b) for(int i=a;i<=b;i++)
# define _Rep(i,a,b) for(int i=a;i>=b;i--)
# define mct(a,b) memset(a,b,sizeof(a))
# define gc getchar()
typedef long long ll;
const int N=1e6+5;
const int inf=0x7fffffff;
const double eps=1e-7;
template <typename T> void read(T &x){
	x=0;int f=1;
	char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+c-'0';
	x*=f;
}

int n,k;
int a[N];
int q[N];
int head=1,tail=1;
int main()
{
	read(n),read(k);
	Rep(i,1,n)read(a[i]);
	Rep(i,1,n){
		if(i-q[head]>=k)head++;
		while(head<=tail&&a[i]>=a[q[tail]])tail--;
		q[++tail]=i;
		if(i>=k)printf("%d\n",a[q[head]]);
	}
	return 0;
}

Headers longer than the main program

Monotone classic example queue

Visiting the exhibition

Cut the cake

Sliding window

For the minimum interval within the m


Monotone queue optimization DP

Monotonous queue there is a very important use is monotonous queue optimization dp

Qi Lu promise

Title Description
fantasy Township, Qi Lu promise is to fool the famous fairy ice.

One day, Qi Lu promise to play again in the frozen frog, the frog is to use ice to freeze up instantly. But the frog ever to be smarter than many, Qi Lu promise before it has run to the other side of the river. Thus the decision to Novo Lu Qi chase frog riverbank.

Stream can be seen as a grid are numbered 0 to N, Qi Connaught exposed only from a small number to a large number of mobile grid lattice. Qi and Lu Connaught move according to a particular way, while she was in a lattice i, she moved to just any one grid interval [i + l, i + r] in. You ask why she was so moving, this is not simply because she is a fool ah.

Each grid has a freezing index A [i], numbered 0 0 index frozen lattice. When exposed Connaught Qi positioned at that grid cells can be obtained that freezing index of A [i]. Qi Lu promise hope to arrive at the other side, for maximum freezing index, so that she can be a harsh lesson frog.

But because she is stupid, so she decided to please you help it decide how to move forward.

Initially, the exposed Qi Connaught on grid number 0, the next position number as long as she is greater than N even reach the other side.

Input format
Line 1: 3 positive integer N, L, R

Line 2: N + 1 integers, the i-th to i-1 denotes the number of grid frozen index A [i-1]

Output formats
an integer index representing the maximum frozen. To ensure that no more than 2 ^ 31-1

Description / prompts
to 100% of the data: N <= 200,000

For all data -1,000 <= A [i] <= 1,000 and 1 <= L <= R <= N


This question is very easy to think of a DP, but a O ( N 2 ) O (N ^ 2) The DP is certainly time to blow

So we have to use monotonous queue, because every point only into the team and the team more than once, so this is monotonous queue optimization DP O ( N ) O (N) of

First, you must start from the L, this time we use now to maintain a current can be achieved at the end of the most i, then that is a sliding window, and this does not go into details.

Note that the loop L from the start

Code:

# include <cstdio>
# include <algorithm>
# include <cstring>
# include <queue>
# include <climits>
# define Rep(i,a,b) for(int i=a;i<=b;i++)
# define _Rep(i,a,b) for(int i=a;i>=b;i--)

using namespace std;

const int N=2e5+5;

int n,l,r,ans=INT_MIN;
int a[N],f[N];
int q[N];
int head=1,tail=1;

int main()
{
	scanf("%d%d%d",&n,&l,&r);
	Rep(i,0,n)scanf("%d",&a[i]);
	memset(f,0xcf,sizeof(f));
	f[0]=0;
	int now=0;
	Rep(i,l,n){
		while(head<=tail&&f[q[tail]]<=f[now])tail--;
		q[++tail]=now;
		while(q[head]+r<i)head++;
		f[i]=f[q[head]]+a[i];
		now++;
	}
	Rep(i,n-r+1,n)ans=max(ans,f[i]);
	printf("%d\n",ans);
	return 0;
}
Published 18 original articles · won praise 3 · Views 1261

Guess you like

Origin blog.csdn.net/devout_/article/details/100164519