zoukankan      html  css  js  c++  java
  • Luogu 2216[HAOI2007]理想的正方形

    Solution

    二维单调队列, 这个数组套起来看得我眼瞎。。。

    Code

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #define rd read()
     5 #define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
     6 #define per(i,a,b) for(register int i = (a); i >= (b); --i)
     7 #define Lb lb[i]
     8 #define Rb rb[i]
     9 #define Ls ls[i]
    10 #define Rs rs[i]
    11 using namespace std;
    12 
    13 const int N = 1e3 + 100;
    14 const int inf = ~0U >> 1;
    15 
    16 int n, m, len, a[N][N];
    17 int qb[N][N], lb[N], rb[N];
    18 int qs[N][N], ls[N], rs[N];
    19 
    20 int qB[N], LB, RB;
    21 int qS[N], LS, RS;
    22 
    23 int ans = inf;
    24 
    25 int read() {
    26     int X = 0, p = 1; char c = getchar();
    27     for(; c > '9' || c < '0'; c = getchar()) if(c == '-') p = -1;
    28     for(; c >= '0' && c <= '9'; c = getchar()) X = X * 10 + c - '0';
    29     return X * p;
    30 }
    31 
    32 int main()
    33 {
    34     n = rd; m = rd; len = rd;
    35     rep(i, 1, n) rep(j, 1, m) a[i][j] = rd;
    36     rep(i, 1, n) ls[i] = lb[i] = 1;
    37     rep(j, 1, m) {
    38         rep(i, 1, n) {
    39             while(qb[i][Lb] <= j - len && Lb <= Rb) Lb++;
    40             while(qs[i][Ls] <= j - len && Ls <= Rs) Ls++;
    41 
    42             while(Lb <= Rb && a[i][qb[i][Rb]] <= a[i][j]) Rb--;
    43             while(Ls <= Rs && a[i][qs[i][Rs]] >= a[i][j]) Rs--;
    44             qb[i][++Rb] = j;
    45             qs[i][++Rs] = j;
    46         }
    47         LS = LB = 1;
    48                 RB = RS = 0;
    49                 if(j >= len)
    50         rep(i, 1, n) {
    51             while(qB[LB] <= i - len && LB <= RB) LB++;
    52             while(qS[LS] <= i - len && LS <= RS) LS++;
    53 
    54             while(LB <= RB && a[qB[RB]][qb[qB[RB]][lb[qB[RB]]]] <= a[i][qb[i][Lb]]) RB--;
    55             while(LS <= RS && a[qS[RS]][qs[qS[RS]][ls[qS[RS]]]] >= a[i][qs[i][Ls]]) RS--;
    56             qB[++RB] = i;
    57             qS[++RS] = i;
    58                         int hb = qB[LB], hs = qS[LS];
    59                         if(i >= len) ans = min(ans, a[hb][qb[hb][lb[hb]]] - a[hs][qs[hs][ls[hs]]]);
    60         }
    61     }
    62     printf("%d
    ", ans);
    63 }
    View Code
  • 相关阅读:
    Java程序,JDK的安装、环境的配置
    Oracle数据库,序列、索引、视图
    Oracle数据库,内置函数小结
    Oracle数据库,join多表关联方式、union结果集合并
    Oracle数据库,模糊查询、去重查询
    Oracle数据库,查询语句、内置函数
    Oracle数据库,数据的增、删、改、查
    Unity 3D-Canvas画布的三种模式
    Unity 3D-Navigation网格导航系统使用教程
    Xlua使用教程、攻略
  • 原文地址:https://www.cnblogs.com/cychester/p/9593144.html
Copyright © 2011-2022 走看看