码迷,mamicode.com
首页 > 其他好文 > 详细

luogu2216 [HAOI2007]理想的正方形

时间:2018-01-17 18:16:12      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:markdown   理想   队列   ret   return   ring   name   min   mini   

先对于每一行中长度为 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;
}

luogu2216 [HAOI2007]理想的正方形

标签:markdown   理想   队列   ret   return   ring   name   min   mini   

原文地址:https://www.cnblogs.com/poorpool/p/8303696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!