1222/2516. buy

Title Description

Description

First you have to admit that today's topic is very simple and very short. . .
Then, you have to admit the following description of the subject more concise! ! !
Task: Given a N * N (1≤N≤2000) matrix also gives an integer K. To you in a given matrix
seeking a sub-matrix, the matrix and the sub-ranges of all the number of this interval to [k, 2 * k].
If there are several such sub-matrix, please feel free to output a.

Input

The first line contains two integers K and N (1≤K≤10 ^ 8,1≤N≤2000). The significance of the topic as described!
Then there are N rows, each row having the number N, the title given matrix. Matrices are in the non-negative and
no greater than maxlongint.

Output

The output file contains only one line, four integers, are the coordinates of the upper left corner of the matrix, you find out the coordinates and the lower right corner.
If there is no such sub-matrix, please output 0000.

Sample Input

Sample Input1:
4 3
1 1 1
1 9 1
1 1 1


Sample Input2:
8 4
1 2 1 3
25 1 2 1
4 20 3 3
3 30 12 2


Sample Input3:
8 4
12 2 1 3
25 1 2 1
4 20 3 3
3 30 12 2

Sample Output

Sample Output1:
0 0 0 0


Sample Output2:
1 2 2 4


Sample Output3:
1 1 1 1

Data Constraint

Hint

Data convention:
For 30% of the data, 1≤N≤5
for 60% of data, 1≤N≤60
data 1≤N≤2000 to 100% of the

answer

Kazumichi God 题

First> number 2k certainly can not choose, so find someone that does not contain> Maximum number of matrix of 2k

With a stack can be O (n ^ 2) obtaining

Then discuss

①sum<k

No solution

②k<=sum<=2k

The current matrix is ​​the solution

③sum>2k

A first row disposed in the matrix for the current and Sum

Then discuss

1、Sum<k

So with the sum-Sum, it will not exceed the boundaries of k , so after subtracting continue

2、k<=Sum<=2k

The current row is the solution

3、Sum>2k

Since the matrix is ​​not guaranteed number> 2k, so the sequence number of the first row deleted

Amassing discuss

Setting a size of the number of deleted

A、a<k

Then Sum-a does not exceed the border, after continuing to lose

B、k<=a<=2k

a solution that is

C、a>2k

does not exist

code

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
using namespace std;

int a[2001][2001];
int f[2001][2001];
long long sum[2001][2001];
int d[2001][2];
int K,K2,n,i,j,k,l,x1,y1,x2,y2,t;
long long mx,Sum;
bool bz;

long long get(int x1,int y1,int x2,int y2)
{
    return sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
}

int main()
{
//  freopen("kup.in","r",stdin);
//  freopen("kup.out","w",stdout);
    
    scanf("%d%d",&K,&n);K2=K*2;
    fo(j,1,n) f[0][j]=1;
    fo(i,1,n)
    {
        fo(j,1,n)
        {
            scanf("%d",&a[i][j]);
            
            if (a[i][j]<=K2)
            f[i][j]=f[i-1][j];
            else
            f[i][j]=i+1;
            
            sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j];
        }
    }
    
    fo(i,1,n)
    {
        t=0;
        fo(j,1,n)
        {
            bz=0;
            while (t && d[t][0]<f[i][j])
            {
                Sum=get(d[t][0],d[t][1],i,j-1);
                if (Sum>mx)
                {
                    mx=Sum;
                    x1=d[t][0];y1=d[t][1];
                    x2=i;y2=j-1;
                }
                
                --t;
                bz=1;
            }
            
            if (f[i][j]<=i && (!t || f[i][j]<d[t][0]))
            {
                ++t;
                d[t][0]=f[i][j];
                if (!bz)
                d[t][1]=j;
            }
        }
        while (t)
        {
            Sum=get(d[t][0],d[t][1],i,n);
            if (Sum>mx)
            {
                mx=Sum;
                x1=d[t][0];y1=d[t][1];
                x2=i;y2=n;
            }
            
            --t;
        }
    }
    
    if (mx<K)
    {
        printf("0 0 0 0\n");
        return 0;
    }
    while (mx>K2)
    {
        Sum=get(x1,y1,x1,y2);
        
        if (Sum>=K)
        {
            x2=x1;
            mx=Sum;
            
            while (mx>K2)
            {
                if (a[x1][y1]<K)
                mx-=a[x1][y1++];
                else
                {
                    mx=a[x1][y1];
                    y2=y1;
                }
            }
        }
        else
        mx-=Sum,++x1;
    }
    
    printf("%d %d %d %d\n",x1,y1,x2,y2);
    
    fclose(stdin);
    fclose(stdout);
    
    return 0;
}

Guess you like

Origin www.cnblogs.com/gmh77/p/11828845.html