luogu2216 [HAOI2007]理想的正方形

先对于每一行中长度为 n 的列用单调队列搞出它们的最小/大值,再将这些长度为 n 的列想象成点再对行跑一遍

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a, b, n, r[1005][1005], qwq[1005], qaq[1005], hwq, twq, haq, taq;
//qwq xiao ,qaq da
int hzxz[1005][1005], hzdz[1005][1005];
//hzxz[i][j]: the minist value in n cols which starts from j in row i
int zzxz[1005][1005], zzdz[1005][1005];
int ans=0x3f3f3f3f, t;
int main(){
    cin>>a>>b>>n;
    for(int i=1; i<=a; i++)
        for(int j=1; j<=b; j++)
            scanf("%d", &r[i][j]);
    for(int i=1; i<=a; i++){
        hwq = haq = 1;
        twq = taq = 0;
        for(int j=1; j<=b; j++){
            t = max(1, j-n+1);
            while(hwq<=twq && qwq[hwq]<=j-n)    hwq++;
            while(haq<=taq && qaq[haq]<=j-n)    haq++;
            while(hwq<=twq && r[i][qwq[twq]]>r[i][j])   twq--;
            while(haq<=taq && r[i][qaq[taq]]<r[i][j])   taq--;
            qwq[++twq] = j;
            qaq[++taq] = j;
            hzxz[i][t] = r[i][qwq[hwq]];
            hzdz[i][t] = r[i][qaq[haq]];
        }
    }
    for(int i=1; i<=b-n+1; i++){
        hwq = haq = 1;
        twq = taq = 0;
        for(int j=1; j<=a; j++){
            t = max(1, j-n+1);
            while(hwq<=twq && qwq[hwq]<=j-n)    hwq++;
            while(haq<=taq && qaq[haq]<=j-n)    haq++;
            while(hwq<=twq && hzxz[qwq[twq]][i]>hzxz[j][i]) twq--;
            while(haq<=taq && hzdz[qaq[taq]][i]<hzdz[j][i]) taq--;
            qwq[++twq] = j;
            qaq[++taq] = j;
            zzxz[t][i] = hzxz[qwq[hwq]][i];
            zzdz[t][i] = hzdz[qaq[haq]][i];
        }
    }
    for(int i=1; i<=a-n+1; i++)
        for(int j=1; j<=b-n+1; j++)
            ans = min(ans, zzdz[i][j]-zzxz[i][j]);
    cout<<ans<<endl;
    return 0;
}

转载于:https://www.cnblogs.com/poorpool/p/8303696.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值