牛客多校__Planting Trees

链接:https://ac.nowcoder.com/acm/contest/883/F
来源:牛客网
 

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

The semester is finally over and the summer holiday is coming. However, as part of your university's graduation requirement, you have to take part in some social service during the holiday. Eventually, you decided to join a volunteer group which will plant trees in a mountain.

 

To simplify the problem, let's represent the mountain where trees are to be planted with an N×NN \times NN×N grid. Let's number the rows 1\ 1 1 to N\ N N from top to bottom, and number the columns 1\ 1 1 to N\ N N from left to right. The elevation of the cell in the i\ i i-th row and j\ j j-th column is denoted by ai,ja_{i,j}ai,j​. Your leader decides that trees should be planted in a rectangular area within the mountain and that the maximum difference in elevation among the cells in that rectangle should not exceed M. In other words, if the coordinates of the top-left and the bottom-right corners of the rectangle are (x1,y1)(x_1,y_1)(x1​,y1​) and (x2,y2)(x_2,y_2)(x2​,y2​), then the condition ∣ai,j−ak,l∣≤M|a_{i,j} - a_{k,l}| \le M∣ai,j​−ak,l​∣≤M must hold for x1≤i,k≤x2, y1≤j,l≤y2x_1 \le i,k \le x_2, \ y_1 \le j,l \le y_2x1​≤i,k≤x2​, y1​≤j,l≤y2​. Please help your leader calculate the maximum possible number of cells in such a rectangle so that he'll know how many trees will be planted.

输入描述:

The input contains multiple cases. The first line of the input contains a single integer T (1≤T≤1000)T \ (1 \le T \le 1000)T (1≤T≤1000), the number of cases.
For each case, the first line of the input contains two integers N (1≤N≤500)N\ (1 \le N \le 500)N (1≤N≤500) and M (0≤M≤105)M\ (0 \le M \le 10^5)M (0≤M≤105). The following N lines each contain N integers, where the j\ j j-th integer in the i\ i i-th line denotes ai,j (1≤ai,j≤105)a_{i,j} \ (1 \le a_{i,j} \le 10^5)ai,j​ (1≤ai,j​≤105).
It is guaranteed that the sum of N3N^3N3 over all cases does not exceed 25⋅10725 \cdot 10^725⋅107.

输出描述:

For each case, print a single integer, the maximum number of cells in a valid rectangle.

示例1

输入

复制

2
2 0
1 2
2 1
3 1
1 3 2
2 3 1
3 2 1

输出

复制

1
4

题意:在一个N*N的矩阵里面找出一个最大的子矩阵满足其中的元素两两之间的差值不超过M

题解:枚举矩阵的上下界,同时维护该列上的最小值和最大值,然后枚举右边界,对于当前的右边界找出最小的左边界,然后相减*上下界的长度,对于怎么找最小的左边界,可以通过边枚举右边界的同时维护两个单调队列,其中一个维护最小值递增的,另外一个维护最大值递减的,这样对于当前新加的值如果更新到了队列最左端(最大值或最小值),(距离是更新到最小值)且此时差值大于M,那么我就要将最大值变小,也就是将最小左边界等于当前最大值的下标加一,然后重复这个判断,就可以知道最小左边界,这样不断更新答案就行了

#include 

using namespace std;

int a[509][509];

int b[509][4];

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;ib[k][1]){
                            tminr--;
                        }
                        quemin[tminr]=b[k][1];
                        indmin[tminr]=k;
                        tminr++;
                    }

                    if(tmaxr==tmaxl){
                        quemax[tmaxr]=b[k][2];
                        indmax[tmaxr]=k;
                        tmaxr++;
                    }
                    else{
                        while(tmaxr!=tmaxl&&quemax[tmaxr-1]m){
//                            cout<<666<m){
                                lmin=indmin[tminl]+1;
                                tminl++;
                            }
                        }
                        else{
                            while(tmaxl!=tmaxr&&abs(quemax[tmaxl]-quemin[tminl])>m){
                                lmax=indmax[tmaxl]+1;
                                tmaxl++;
                            }
                        }
                    }
                    int p=max(lmin,lmax);
                    ans=max(ans,(k-p+1)*(j-i+1));
//                    cout<

 

你可能感兴趣的:(单调队列,牛客网,矩阵)