2019牛客暑期多校训练营(第三场) Planting Trees

时间限制: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 grid. Let's number the rows 1 to N from top to bottom, and number the columns 1 to N from left to right. The elevation of the cell in the i-th row and j-th column is denoted by 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) and (x2,y2), then the condition ∣ai,j−ak,l∣≤M must hold for x1≤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), the number of cases.
For each case, the first line of the input contains two integers N (1≤N≤500) and M (0≤M≤10^5). The following N lines each contain N integers, where the j-th integer in the i-th line denotes ai,j (1≤ai,j≤10^5).
It is guaranteed that the sum of N^3 over all cases does not exceed 25*10^7.

输出描述:

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

输入

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

输出

1 
4

题意:找到一个最大子矩阵面积,条件为矩阵内的最大值与最小值的差值应该小于等于k。

题解:这道题如果不太会做,可以先试试hdu 3530  Subsequence,这道题思想与本题相同并且相对简单。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=505,inf=0x3f3f3f3f;
int MAX[maxn],MIN[maxn],g[maxn][maxn],quemax[maxn],quemin[maxn];
int main()
{
  int i,j,T,n,m,up,down,left,right,ans,hmax,tmax,hmin,tmin;
  scanf("%d",&T);
  while(T--)
  {
    ans=-1;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
     for(j=1;j<=n;j++)
      scanf("%d",&g[i][j]);
    for(up=1;up<=n;up++)
    {
      fill(MAX,MAX+maxn,-inf);
      fill(MIN,MIN+maxn,inf);
      for(down=up;down<=n;down++)
      {
        left=0;hmax=hmin=0;tmax=tmin=-1;
        for(i=1;i<=n;i++) 
        {
          MAX[i]=max(MAX[i],g[down][i]);
          MIN[i]=min(MIN[i],g[down][i]);
        }
        for(right=1;right<=n;right++)
        {
          while(tmax>=hmax&&MAX[right]>=MAX[quemax[tmax]]) tmax--;
          quemax[++tmax]=right;
          while(tmin>=hmin&&MIN[right]<=MIN[quemin[tmin]]) tmin--;
          quemin[++tmin]=right;
          while(tmax>=hmax&&tmin>=hmin&&MAX[quemax[hmax]]-MIN[quemin[hmin]]>m)
          {
            if(quemax[hmax]<quemin[hmin]) left=quemax[hmax],hmax++;
            else left=quemin[hmin],hmin++;
          }
          ans=max(ans,(down-up+1)*(right-left));
        }
      }
    }
   printf("%d\n",ans);
  }
  system("pasue");
  return 0;
}
posted @ 2019-08-20 17:23  Vivid-BinGo  阅读(186)  评论(0编辑  收藏  举报