Planting Trees

Planting Trees

Time limit: C / C ++ 3 seconds to 6 seconds languages other
space restrictions: C / C ++ 262144K, other languages 524288K
64bit IO the Format:% LLD

Title Description

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×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.

Enter a description:

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≤105). The following N lines each contain N integers, where the  j-th integer in the  i-th line denotes ai,j (1≤ai,j≤105).
It is guaranteed that the sum of N3N^3N3 over all cases does not exceed 25⋅1e7 .

Output Description:

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

Entry

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

Export

1 
4 

link: https://ac.nowcoder.com/acm/contest/883/F
Source: Cattle-off network

That Italy: choose a two-dimensional matrix of numbers in a sub-matrix, and the absolute value of the difference of two numbers in any sub-matrix M or less, the maximum Child-giving matrix area.
Thinking 1: Consider a monotone stack done before seeking the maximum full title of a sub-matrix, this problem can also be converted to this problem, i.e. to ensure each of two arbitrarily large number of matrix inside the absolute value of the difference is less than or equal M, then the reserved number 1 becomes down, the remaining number becomes zero, find the maximum full matrix sub-matrix. The specific method is the matrix in accordance with the number of sorted weights, left and right pointers maintained, while each deletion of some number plus some number, but the total number in the current matrix to ensure the absolute value of the difference is less than equal to M. Also a problem is the details: the number of each modification when there are only a few specific lines will affect the answer, so we just have to run it a few lines on the line, you do not need to run the whole sub-maximum of the entire matrix of the matrix.

#include <bits/stdc++.h>
using namespace std;
  
const int N =550;
  
long long check(int a[],int n)
{
    int p;
    long long ans=0;
    a[n+1]=p=0;
    int s[N]={0};
    int w[N]={0};
    for(int i=1;i<=n+1;i++)
    {
        if(a[i]>s[p])
        {
            s[++p] = a[i];
            w[p] = 1;
        }
        else
        {
            int width=0;
            while(s[p]>a[i])
            {
                width+=w[p];
                ans=max(ans,(long long)width*s[p]);
                p--;
            }
            s[++p]=a[i];
            w[p]=width+1;
        }
  
    } 
     )Return ANS; 
} 
  
struct SS 
{ 
    int X, Y, A; 
  
    BOOL  operator <( const SS & S) const 
    { 
        return A < SA; 
    } 
}; 
  
SS ARR [N * N];
 int the Map [N] [N] ;
 int VIS [N] = { 0 }; 
  
void Deal ( int X, int Y, int n-, int type) // modify a number, it will also influence the number of the following 
{
     IF (type == 0 
    {
        Map[x][y]=0;
        x++;
  
        while(x<=n&&Map[x][y])
        {
            Map[x][y]=Map[x-1][y]+1;
            x++;
        }
  
    } else
    {
        vis[x]=1;
        Map[x][y]=Map[x-1][y]+1;
        x++;
  
        while(x<=n&&Map[x][y])
        {
            vis[x]=1;
            Map[x][y] = Map[x - 1][y] + 1;
            x++;
        }
    }
}
  
  
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        long long ans=1;
        int tot=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                Map[i][j]=0;
                scanf("%d",&arr[++tot].a);
                arr[tot].x=i;
                arr[tot].y=j;
            }
        }
  
        sort(arr+1,arr+1+tot);
        int l=1,r=2;
  
        Deal (ARR [ . 1 ] .x, ARR [ . 1 ] .y, n-, . 1 ); 
  
        the while (R & lt <* = n- n-) 
        { 
            the while (R & lt <= n-n-&& * ARR [R & lt] II.A-ARR [L]. A <= m) 
            { 
                Deal (ARR [R & lt] .x, ARR [R & lt] .y, n-, 1 ); // increase a number of 
                R & lt ++ ; 
            } 
 
            for ( int I = 1 ; I <= n-; I ++ ) 
            { 
                IF (VIS [I]) // run the whole sub-maximum possible answers for all rows impact matrix 
                { 
                    VIS [I] = 0 ; 
                    ANS=max(ans,check(Map[i],n));
                }
            }
  
            int last=l;
            while(l<r&&arr[l].a==arr[last].a)//删除一个数
            {
                deal(arr[l].x,arr[l].y,n,0);
                l++;
            }
  
        }
        printf("%lld\n",ans);
    }
    return 0;
}
Solution 1

 

Ideas 2: Enumeration sub-rectangular upper and lower boundaries, while maintaining the maximum and minimum values ​​for each column of the upper and lower boundaries of the current enumeration. After determining the upper and lower bounds, except for Creating the right border of the rectangle, then consider monotonous stack quickly obtain a viable minimum left margin. Enumeration right boundary monotone maintain two queues, respectively maximum and minimum maintenance, minimum queue subscripts monotonically increase, the size of the increment, a maximum value of the subscript increasing, decreasing size. Using these two monotonous queue can maintain a current minimum viable left margin.

#include <bits/stdc++.h>
#define inf (INT_MAX/2)
using namespace std;

const int N = 550;
int arr[N][N];

int ma[N],mi[N];

struct ss
{
    int v,index;
};

struct Deque{

    ss arr[N];
    int l,r;

    Deque()
    {
        l=0;
        r=0;
    }

    void clear()
    {
        l=r=0;
    }

    void push_back(ss now)
    {
        arr[r++]=now;
    }
    void pop_back()
    {
        r--;
    }
    void pop_front()
    {
        l++;
    }
    ss front()
    {
        return arr[l];
    }
    ss back()
    {
        return arr[r-1];
    }

    bool empty()
    {
        return l==r;
    }
};


Deque mas,mis;

void clear()
{
    mas.clear();
    mis.clear();
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d %d",&n,&m);

        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                scanf("%d",&arr[i][j]);

        int ans=0;

        for(int upper=1; upper<=n; upper++)
        {
            for(int i=1; i<=n; i++)
            {
                ma[i]=0;
                mi[i]=inf;
            }

            for(int down=upper; down<=n; down++)
            {
                clear();
                for(int i=1; i<=n; i++)
                {
                    mA [I]=max (mA [I], ARR [Down] [I]); 
                    mi The [I] = min (mi The [I], ARR [Down] [I]); 

                    IF (mA [I] -mi [I]> m ) 
                    { 
                        Clear (); 
                        Continue ; 
                    } 
                    int now = I;
                     the while ! (mas.empty ().. && mas.back () V <= mA [I]) mas.back = now () index, mas.pop_back ( ); // the number smaller than the maximum value of the right boundary, the right boundary if the maximum value that can be included into a sub-matrix, the number may be certainly contain 
                    the while (mis.empty (!) && ABS (mA [I] . -mis.front () V)> m) mis.pop_front (); // not meet the condition deletion 
                    mas.push_back ((SS) {mA [I], now}); 

                    now =I;
                     the while (! mis.empty () && mis.back () V> = mi The [I].) mis.back = now () index, mis.pop_back ();. // The right boundary than the minimum number of large, then the right boundary if the minimum that can be included into a sub-matrix, the number may be certainly contain 
                    the while (! mas.empty () && ABS (mas.front (). V-mi the [I])> m) mas.pop_front (); 
                    mis.push_back ((SS) {mi The [I], now}); 
                    ANS = max (ANS, (I + . 1 -max (mis.front () index, mas.front () index.. )) * (- Upper Down + + . 1 )); 
                } 
            } 
        } 
        the printf ( " % D \ n- " , ANS); 
    } 
    return  0 ; 
}
Solution 2

 

 
    

Guess you like

Origin www.cnblogs.com/tian-luo/p/11249240.html