2019HDU multi-school training third Planting Trees violence monotonous queue optimization +

The meaning of problems: there is a n * n grid, each intermediate grid have a tree, you know every high tree, you can select a rectangular area inside the fenced trees, but any two rectangular areas inside the absolute value of the difference between the height of the tree does not exceed m, the maximum area of ​​this rectangle ask is how much?

Ideas: more than two days before the cattle off the school has one of the largest sub-rectangle problem, then scan line with the + stack too monotonous, the results of the field for a long time wanted to destroy the results of QAQ. This problem has restrictions on the bad do. Note the information in the title, you can use O (n ^ 3) algorithm to do, if we enumerate the left and right corners of the matrix is ​​O (n ^ 4), and no optimization methods, no. But we converted some ideas, we enumerate rectangular upper and lower boundaries and the right boundary, there is no way we think about finding an optimal corresponds to the left edge of the right border shared equally in time O (1). How to find the left edge of it? First we have determined difference between the current left and the other three boundary rectangle surrounded by maximum and minimum values ​​are not more than m, if no instructions are valid. Maximum and minimum maintenance interval, a variety of data structures, but monotonous queue is cost-effective, because each increase of only one right border, and a high degree of fit monotone queues and shared equally achieve O (1). Well, we maintain monotonous queue and left margin of a minimum and a maximum. After moving a right boundary, the right side of the boundary between the upper and lower boundaries of the column where the minimum and maximum resolution pressed into two monotone queue, if the difference is within the current rectangle is greater than m, moves the left margin. If monotonous queue position is less than the left edge of the first team to pop, update the answer.

Code:


					mi[k] = min(mi[k], a[j][k]);
					mx[k] = max(mx[k], a[j][k]);
					while(l1 <= r1 && mi[q1[r1]] > mi[k]) r1--;
					q1[++r1] = k;
					while(l2 <= r2 && mx[q2[r2]] < mx[k]) r2--;
					q2[++r2] = k;
					while(pos <= k && mx[q2[l2]] - mi[q1[l1]] > m) {
						pos++;
						if(q1[l1] < pos) l1++;
						if(q2[l2] < pos) l2++;
					}
					if(l1 <= r1 && l2 <= r2) {
						ans = max(ans, (j - i + 1) * (k - pos + 1));
					}
				}
			}
		}
		printf("%d\n", ans);
	}
} 

  

Guess you like

Origin www.cnblogs.com/pkgunboat/p/11247387.html
Recommended