bzoj1047: [HAOI2007] Ideal Square

Source: Internet
Author: User

Good God's monotonous stack ah ...

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace STD, #define REP (i,s,t) for (Register int. i=s;i<=t;i++) #define DWN (i,s,t) for (register int i=s;i>=t;i--) #define CLR    (x,c) memset (x,c,sizeof (x)) int read () {int X=0;char c=getchar ();    while (!isdigit (c)) C=getchar ();    while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return x;} const int Nmax=1005;const int Inf=0x7f7f7f7f;int a[nmax][nmax],n,m,b;int q[nmax],mx[nmax][nmax],mn[nmax][nmax],ans[        Nmax],res[nmax];void Solve () {rep (i,1,n) {int l=1,r=0;            Rep (j,1,m) {if (q[l]<=j-b) l++;            while (L<=r&&a[i][q[r]]<=a[i][j]) r--;            Q[++r]=j;        if (j>=b) mx[i][j]=a[i][q[l]];        } l=1,r=0;            Rep (j,1,m) {if (q[l]<=j-b) l++;            while (L<=r&&a[i][q[r]]>=a[i][j]) r--;            Q[++r]=j;        if (j>=b) mn[i][j]=a[i][q[l]]; }}/*rep (I,1,n) {REP (j,b,m) printf ("%d", Mx[i][j]);p rintf ("\ n");    } printf ("\ n");    Rep (I,1,n) {rep (j,b,m) printf ("%d", Mn[i][j]);p rintf ("\ n"); } printf ("\ n"); */}void mins (int &a,int b) {if (a>b) a=b;}    void work () {int ret=inf;        Rep (j,b,m) {int l=1,r=0;            Rep (i,1,n) {if (q[l]<=i-b) l++;            while (L<=r&&mx[q[r]][j]<=mx[i][j]) r--;            Q[++r]=i;        if (i>=b) ans[i]=mx[q[l]][j];        } l=1,r=0;            Rep (i,1,n) {if (q[l]<=i-b) l++;            while (L<=r&&mn[q[r]][j]>=mn[i][j]) r--;            Q[++r]=i;        if (i>=b) res[i]=mn[q[l]][j];    } Rep (i,b,n) mins (ret,ans[i]-res[i]); } printf ("%d\n", ret);}    int main () {N=read (), M=read (), B=read ();    Rep (I,1,n) Rep (j,1,m) A[i][j]=read ();    Solve (); work (); return 0;}

  

1047: [HAOI2007] Ideal square time limit:10 Sec Memory limit:162 MB
submit:2702 solved:1465
[Submit] [Status] [Discuss] Description

There is a matrix of a*b integers, now ask you to find a n*n square area, so that the maximum and minimum values of all the numbers in the region
The difference is minimal.

Input

The first behavior is 3 integers, which represent the value of A,b,n the second row to the a+1 line each behavior b nonnegative integer, representing the number in the corresponding position in the matrix. Every
Rows are separated by a space between two adjacent numbers.
100% of Data 2<=a,b<=1000,n<=a,n<=b,n<=100

Output

Only one integer, which is the minimum value of the difference between the maximum and minimum integers in all the n*n square areas in the a*b matrix.

Sample Input5 4 2
1 2 5 6
0 17 16 0
16 17 2 1
2 10 2 1
1 2 2 2Sample Output1HINT Source [Submit] [Status] [Discuss]

bzoj1047: [HAOI2007] Ideal square

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.