Ideal square (sliding window)

Insert picture description here

Idea: Find the minimum and maximum values ​​of a sliding window of length k for each row, then find the column again, and start traversing at k*k.

#pragma GCC optimize(2)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<iomanip>
#include<cstring>
#include<time.h>
 
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e9+7;
const int N=2e6+10;
const int M=1e5+10;
const int inf=0x3f3f3f3f;
const int maxx=2e5+7;
const double eps=1e-6;
int gcd(int a,int b)
{
    
    
    return b==0?a:gcd(b,a%b);
}
 
ll lcm(ll a,ll b)
{
    
    
    return a*(b/gcd(a,b));
}
 
template <class T>
void read(T &x)
{
    
    
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    
    
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
         write(x / 10);
    putchar('0' + x % 10);
}
ll qsm(int a,int b,int p)
{
    
    
    ll res=1%p;
    while(b)
    {
    
    
        if(b&1)
            res=res*a%p;
        a=1ll*a*a%p;
        b>>=1;
    }
    return res;
}
int n, m,k;
int dp[N];
int q[N];
int w[1005][1005];
int rmax[1005][1005],rmin[1005][1005];
void getmin(int a[],int b[],int len)
{
    
    
    int hh=0,tt=-1;
    for(int i=1;i<=len;i++)
    {
    
    
        if(hh<=tt&&q[hh]<=i-k)hh++;
        while(hh<=tt&&a[q[tt]]>=a[i])tt--;
        q[++tt]=i;
        b[i]=a[q[hh]];
    }

}
void getmax(int a[],int b[],int len)
{
    
    
    int hh=0,tt=-1;
    for(int i=1;i<=len;i++)
    {
    
    
        if(hh<=tt&&q[hh]<=i-k)hh++;
        while(hh<=tt&&a[q[tt]]<=a[i])tt--;
        q[++tt]=i;
        b[i]=a[q[hh]];
    }
}
int main()
{
    
    
    
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;i++){
    
    
        for(int j=1;j<=m;j++){
    
    
            scanf("%d",&w[i][j]);
        }
    }
    int a[1005],b[1005],c[1005];
    for(int i=1;i<=n;i++){
    
    
        getmin(w[i],rmin[i],m);
        getmax(w[i],rmax[i],m);
    }
    int res=inf;
    for(int i=k;i<=m;i++)
    {
    
    
        for(int j=1;j<=n;j++)
          a[j]=rmin[j][i];
        getmin(a,b,n);
        for(int j=1;j<=n;j++)
        a[j]=rmax[j][i];
        getmax(a,c,n);
        for(int j=k;j<=n;j++) res=min(res,c[j]-b[j]);
    }
    printf("%d",res);

    return 0;

}


Guess you like

Origin blog.csdn.net/qq_43619680/article/details/109393791