传送门

蛤省省选果然水啊,我这种蒟蒻都能一遍A。

横向纵向维护两个单调队列,做两次求最大和最小的,总复杂度$O(NM)$

码农题,考察代码实现能力

 //BZOJ 1047
 //by Cydiater
 //2016.9.17
 #include <iostream>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <cmath>
 #include <ctime>
 #include <map>
 #include <iomanip>
 #include <string>
 #include <algorithm>
 #include <queue>
 using namespace std;
 #define ll long long
 #define up(i,j,n)        for(int i=j;i<=n;i++)
 #define down(i,j,n)        for(int i=j;i>=n;i--)
 ;
 const int oo=0x3f3f3f3f;
 inline int read(){
     ,f=;
     ;ch=getchar();}
     +ch-';ch=getchar();}
     return x*f;
 }
 int N,M,K,a[MAXN][MAXN],head[MAXN],tail[MAXN],lable_max[MAXN][MAXN],lable_min[MAXN][MAXN],q[MAXN][MAXN],extro_q[MAXN],extro_head,extro_tail,ans=oo;
 namespace solution{
     inline void push(int co,int id){q[co][++tail[co]]=id;}
     void init(){
         N=read();M=read();K=read();
         up(i,,N)up(j,,M)a[i][j]=read();
     }
     void Queue_max(){
         memset(tail,,sizeof(tail));
         up(i,,N)head[i]=;
         up(i,,N)up(j,,K){
             while(head[i]<=tail[i]&&a[i][j]>a[i][q[i][tail[i]]])tail[i]--;
             push(i,j);//load y
         }
         extro_head=;extro_tail=;
         up(i,,K-){
             while(extro_head<=extro_tail&&a[i][q[i][head[i]]]>a[extro_q[extro_tail]][q[extro_q[extro_tail]][head[extro_q[extro_tail]]]])extro_tail--;
             extro_q[++extro_tail]=i;//load x
         }
         up(i,K,N){
             >K&&extro_head<=extro_tail)extro_head++;
             while(extro_head<=extro_tail&&a[i][q[i][head[i]]]>a[extro_q[extro_tail]][q[extro_q[extro_tail]][head[extro_q[extro_tail]]]])extro_tail--;
             extro_q[++extro_tail]=i;//load x
             lable_max[i][K]=a[extro_q[extro_head]][q[extro_q[extro_head]][head[extro_q[extro_head]]]];
         }
         up(j,K+,M){
             up(i,,N){
                 >K&&head[i]<=tail[i])head[i]++;
                 while(head[i]<=tail[i]&&a[i][j]>a[i][q[i][tail[i]]])tail[i]--;
                 push(i,j);//load y
             }
             extro_head=;extro_tail=;
             up(i,,K-){
                 while(extro_head<=extro_tail&&a[i][q[i][head[i]]]>a[extro_q[extro_tail]][q[extro_q[extro_tail]][head[extro_q[extro_tail]]]])extro_tail--;
                 extro_q[++extro_tail]=i;//load x
             }
             up(i,K,N){
                 >K&&extro_head<=extro_tail)extro_head++;
                 while(extro_head<=extro_tail&&a[i][q[i][head[i]]]>a[extro_q[extro_tail]][q[extro_q[extro_tail]][head[extro_q[extro_tail]]]])extro_tail--;
                 extro_q[++extro_tail]=i;//load x
                 lable_max[i][j]=a[extro_q[extro_head]][q[extro_q[extro_head]][head[extro_q[extro_head]]]];
             }
         }
     }
     void Queue_min(){
         memset(tail,,sizeof(tail));
         up(i,,N)head[i]=;
         up(i,,N)up(j,,K){
             while(head[i]<=tail[i]&&a[i][j]<a[i][q[i][tail[i]]])tail[i]--;
             push(i,j);//load y
         }
         extro_head=;extro_tail=;
         up(i,,K-){
             while(extro_head<=extro_tail&&a[i][q[i][head[i]]]<a[extro_q[extro_tail]][q[extro_q[extro_tail]][head[extro_q[extro_tail]]]])extro_tail--;
             extro_q[++extro_tail]=i;//load x
         }
         up(i,K,N){
             >K&&extro_head<=extro_tail)extro_head++;
             while(extro_head<=extro_tail&&a[i][q[i][head[i]]]<a[extro_q[extro_tail]][q[extro_q[extro_tail]][head[extro_q[extro_tail]]]])extro_tail--;
             extro_q[++extro_tail]=i;//load x
             lable_min[i][K]=a[extro_q[extro_head]][q[extro_q[extro_head]][head[extro_q[extro_head]]]];
         }
         up(j,K+,M){
             up(i,,N){
                 >K&&head[i]<=tail[i])head[i]++;
                 while(head[i]<=tail[i]&&a[i][j]<a[i][q[i][tail[i]]])tail[i]--;
                 push(i,j);//load y
             }
             extro_head=;extro_tail=;
             up(i,,K-){
                 while(extro_head<=extro_tail&&a[i][q[i][head[i]]]<a[extro_q[extro_tail]][q[extro_q[extro_tail]][head[extro_q[extro_tail]]]])extro_tail--;
                 extro_q[++extro_tail]=i;//load x
             }
             up(i,K,N){
                 >K&&extro_head<=extro_tail)extro_head++;
                 while(extro_head<=extro_tail&&a[i][q[i][head[i]]]<a[extro_q[extro_tail]][q[extro_q[extro_tail]][head[extro_q[extro_tail]]]])extro_tail--;
                 extro_q[++extro_tail]=i;//load x
                 lable_min[i][j]=a[extro_q[extro_head]][q[extro_q[extro_head]][head[extro_q[extro_head]]]];
             }
         }
     }
     void slove(){
         Queue_max();
         Queue_min();
     }
     void output(){
         /*up(i,1,N){
             up(j,1,M)cout<<lable_max[i][j]<<' ';
             cout<<endl;
         }
         puts("");
         up(i,1,N){
             up(j,1,M)cout<<lable_min[i][j]<<' ';
             cout<<endl;
         }
         puts("");*/
         up(i,K,N)up(j,K,M)ans=min(ans,lable_max[i][j]-lable_min[i][j]);
         cout<<ans<<endl;
     }
 }
 int main(){
     //freopen("input.in","r",stdin);
     using namespace solution;
     init();
     slove();
     output();
     ;
 }

妈的,下次再也不起这么长的变量名了,敲到手麻

BZOJ1047: [HAOI2007]理想的正方形的更多相关文章

  1. BZOJ1047: [HAOI2007]理想的正方形 [单调队列]

    1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2857  Solved: 1560[Submit][St ...

  2. bzoj千题计划215:bzoj1047: [HAOI2007]理想的正方形

    http://www.lydsy.com/JudgeOnline/problem.php?id=1047 先用单调队列求出每横着n个最大值 再在里面用单调队列求出每竖着n个的最大值 这样一个位置就代表 ...

  3. [bzoj1047][HAOI2007]理想的正方形_动态规划_单调队列

    理想的正方形 bzoj-1047 HAOI-2007 题目大意:有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 注释:$2\le a, ...

  4. BZOJ1047[HAOI2007]理想的正方形——二维ST表

    题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入 第一行为3个整数,分别表示a,b,n的值第二行至第a+1行每行为b个非 ...

  5. [BZOJ1047][HAOI2007]理想的正方形(RMQ+DP)

    题意 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 思路 RMQ求 再DP 代码 #include<cstdio> #i ...

  6. 【单调队列】bzoj1047 [HAOI2007]理想的正方形

    先把整个矩阵处理成b[n][m-K+1].c[n][m-K+1]大小的两个矩阵,分别存储每行每K个数中的最大.最小值,然后再通过b.c处理出d.e分别表示K*K大小的子矩阵中的最大.最小值即可.单调队 ...

  7. [BZOJ1047][HAOI2007]理想的正方形 二维单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 我们对每矩阵的一列维护一个大小为$n$的单调队列,队中元素为矩阵中元素.然后扫描每一 ...

  8. bzoj1047 [HAOI2007]理想的正方形——二维单调队列

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1047 就是先对行做一遍单调队列,再对那个结果按列做一遍单调队列即可. 代码如下: #incl ...

  9. [Bzoj1047][HAOI2007]理想的正方形(ST表)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1047 题目虽然有一个n的限制,但求二维区间最值首先想到的还是RMQ,但是如果按照往常RM ...

随机推荐

  1. JS 问题集锦

    [1]js页面跳转 和 js打开新窗口方法 第一种: <script language="javascript" type="text/javascript&quo ...

  2. VBPR: Visual Bayesian Personalized Ranking from Implicit Feedback-AAAI2016 -20160422

    1.Information publication:AAAI2016 2.What 基于BPR模型的改进:在商品喜好偏序对的学习中,将商品图片的视觉信息加入进去,冷启动问题. 3.Dataset Am ...

  3. 枚举型Enum和结构型Stuct

    枚举型实质就是使用符号来表示的一组相互关联的数据. Season currentSeason,nextSeason; currentSeason = Season.Spring; nextSeason ...

  4. strut2的拦截器之对request和session的封装

    本文主要内容是对  implements Interceptor 这种方式的探索 前提是需要了解:责任链模式 对下面这句代码的理解: Map<String, Object> session ...

  5. 献给那些每次调试时都要启动很多WEB项目的苦逼程序猿

    当一个解决方案包含多个WEB项目的时候,只要按F5调试,其它用不着的WEB项目也会自动添加到托盘里.很多新手都不知道如何解决这个问题,我也是刚知道. 在网上找了很多资料看到有2种解决方法: 1.把WE ...

  6. Oracle:高效插入大量数据经验之谈

    来源于:http://www.cnblogs.com/liwenzhen238/p/3610518.html 在很多时候,我们会需要对一个表进行插入大量的数据,并且希望在尽可能短的时间内完成该工作,这 ...

  7. Percona 开始尝试基于Ceph做上层感知的分布式 MySQL 集群,使用 Ceph 提供的快照,备份和 HA 功能来解决分布式数据库的底层存储问题

    本文由 Ceph 中国社区 -QiYu 翻译 英文出处:Using Ceph with MySQL 欢迎加入CCTG Over the last year, the Ceph world drew m ...

  8. 链栈的C语言实现

    /* 功能:栈的链表实现 Author:lxm Date: 20160511 */ #include <stdio.h> #include <stdlib.h> #define ...

  9. Jenkins_多项目构建(一):单独建立一个项目按顺序执行其它job

    有A,B,C三个Job,A为服务或web Job,B,C为A依赖的其它Job 单独建个Job,按B,C,A的顺序进行编译   1.安装插件:Multijob plugin   2.新建A,B,C三个J ...

  10. bzoj1787

    lca裸题,画画图看看就可以了,找出那个一次公共祖先,求距离 #include<iostream> #include<set> #include<cstring> ...