zoukankan      html  css  js  c++  java
  • 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
    ",ans);
      }
      system("pasue");
      return 0;
    }
    本博客仅为本人学习,总结,归纳,交流所用,若文章中存在错误或有不当之处,十分抱歉,劳烦指出,不胜感激!!!
  • 相关阅读:
    css最简单的在背景图片上显示模糊背景色的方法
    css添加网格背景
    获取bing必应图片
    JavaScript超过一定高度导航添加类名
    6行css就可以解决的瀑布流布局的方法
    css实现背景图横向滚动
    JavaScript根据一个元素的显示隐藏控制另一个元素的显示和隐藏
    JavaScript判断地址栏链接与导航栏链接是否一致并给导航添加class
    JavaScript实现选中文字自动复制
    Day 74 算法基础(二)
  • 原文地址:https://www.cnblogs.com/VividBinGo/p/11384146.html
Copyright © 2011-2022 走看看