P2216 [haoi2007] ideal square-monotonic queue

Source: Internet
Author: User

This type of rectangle problem is often used to optimize the enumeration of monotonous queues (which reduces the enumeration of one dimension through greedy optimization)
We recommend that you do the following: [zjoi2007] Board creation
Remember to increase the space of the monotonous queue!Enough memory anyway
Note that the square side length is fixed!
The brute-force algorithm is used to enumerate the rows with the upper boundary and the columns with the left boundary. The two information is used to determine a square, then pre-process the maximum value of a row from vertex I to vertex I + N, and then scan the row of this square again. The complexity is n ^ 3.

During preprocessing, the complexity is also n ^ 3... Consider using a monotonous queue for maintenance, enumerating a row, and looking at it as a sequence. Finding the maximum value of the range with the length of N is a sliding window. If you do not understand it, do the following: p1886 Sliding Window

You can use a monotonous queue to optimize an enumeration layer when you want to obtain the answer. You do not need to enumerate rows. You only need to maintain a monotonous queue during column enumeration becauseThe side length of a square is fixed., You only need to know what the column is, you can know the row

Taking the maximum value as an example: When enumerating a column, you can directly scan the row, didn't we pre-process a f [I] [J] to indicate the maximum value of the next n elements including the element in column J of row I, put it in a monotonically decreasing queue and maintain that the queue length does not exceed n. The maximum value of this square is the queue header.

Think about it. If one element is larger than the one in the queueAfterWhen I calculate the maximum value, I don't need to know the specific elements before this element, because there is a bigger element that can survive for a longer time, so the previous elements can be discarded, simply leave the team and maintain timeliness and adequacy

!!! Note: Before updating the answer, you must ensure that there are only n elements in the queue, that is, you must add a special sentence I> = n, otherwise, the answer will be updated without a square.

# Include <algorithm> # include <iostream> # include <cstring> # include <cstdio> # include <queue> # include <cmath> using namespace STD; # define debug (X) cerr <# x <"=" <x <Endl; const int maxn = 1000 + 10; const int INF = 1 <30; typedef long ll; int n, m, a, B, ANS = inf, max_l, max_r, min_l, min_r; int f [maxn] [maxn], G [maxn] [maxn], REC [maxn] [maxn]; struct st {int Val, Pos;} Qmax [2 * maxn], qmin [2 * maxn]; void max_modify (int K, int POS) {While (max_l <= max_r & Qmax [max_r]. val <= k) max_r --; Qmax [++ max_r]. val = K, Qmax [max_r]. pos = Pos; while (max_l <= max_r & ABS (Qmax [max_r]. pos-Qmax [max_l]. pos) + 1> N) max_l ++;} void min_modify (int K, int POS) {While (min_l <= min_r & qmin [min_r]. val> = k) min_r --; qmin [++ min_r]. val = K, qmin [min_r]. pos = Pos; while (min_l <= min_r & ABS (qmin [min_r]. pos-qmin [min_l]. pos) + 1> N) min_l ++;} void Init () {for (INT I = 1; I <= A; I ++) {max_l = 1, max_r = 0; // reset the queue. Otherwise, the space must be scaled n ^ 2; otherwise, the queue will be RE; think about why min_l = 1, min_r = 0; for (Int J = B; j --) {max_modify (REC [I] [J], J); min_modify (REC [I] [J], J ); // one time for each node to enter the team. f [I] [J] = Qmax [max_l]. val; G [I] [J] = qmin [min_l]. val ;}} void work () {for (Int J = N; j <= B; j ++) {int Pos = J-n + 1; max_l = 1, max_r = 0; min_l = 1, min_r = 0; For (INT I = 1; I <= A; I ++) {max_modify (F [I] [POS], i); min_modify (G [I] [POS], I); if (I> = N) ans = min (ANS, Qmax [max_l]. val-qmin [min_l]. val) ;}} int main () {scanf ("% d", & A, & B, & N); For (INT I = 1; I <= A; I ++) {for (Int J = 1; j <= B; j ++) {scanf ("% d ", & rec [I] [J]) ;}} Init (); Work (); printf ("% d \ n", ANS); Return 0 ;}

P2216 [haoi2007] ideal square-monotonic queue

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.