1047: [HAOI2007] Ideal square time limit:10 Sec Memory limit:162 MB
submit:2230 solved:1188
[Submit] [Status] [Discuss] Description
There is a matrix of a*b integers, now you find a n*n square area, which minimizes the difference between the maximum and minimum values in all numbers in the region.
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. Each row is separated by a space between two adjacent numbers.
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
Problem size
(1) No more than 1,000,000,000 of all the numbers in the matrix
(2) 20% data 2<=a,b<=100,n<=a,n<=b,n<=10
(3) 100% data 2<=a,b<=1000,n<=a,n<=b,n<=100
Source
This problem has a lot of solutions, I used the efficiency of the two-dimensional not optimized RMQ (hehe can ac is a miracle), specifically, with $maxm[i][j][k]$ to represent the top left corner is $ (i,j) $, the size of the $2^k*2^k$ square in the maximum value, the minimum value of the same processing. After a one-dimensional case, multiply preprocessing, and note the use of long long . The last output enumerates the upper-left position, calculates and outputs, and the total time complexity is probably $o (Ablog n+ab) $.
/************************************************************** problem:1047 USER:BHIAIB0GF language:c++ result:accepted time:4420 Ms memory:110876 kb****************************************************************/#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>using namespacestd;#defineLOG2 (x) (log (x)/log (2))Long Longmaxm[1001][1001][7],minm[1001][1001][7];intA,b,n;intReadint () {intans=0; CharC; while(!isdigit (c=GetChar ())); Do{ans=ans*Ten+c-'0'; C=GetChar (); } while(IsDigit (c)); returnans;}intMain () {a=readint (), B=readint (), n=Readint (); for(intI=1; i<=a;i++) for(intj=1; j<=b;j++) {maxm[i][j][0]=Readint (); minm[i][j][0]=maxm[i][j][0]; } for(inti1=1; i1<=log2 (n); i1++) { for(intI=1; i+ (1<<I1)-1<=a;i++) for(intj=1; j+ (1<<I1)-1<=b;j++) {MAXM[I][J][I1]=Max (max (Maxm[i][j][i1-1], Maxm[i+(1<< (i1-1))][j][i1-1]), Max (Maxm[i][j+(1<< (i1-1))][i1-1], Maxm[i+(1<< (i1-1))][j+ (1<< (i1-1))][i1-1] ) ); MINM[I][J][I1]=min (min (minm[i][j][i1-1], Minm[i+(1<< (i1-1))][j][i1-1]), min (minm[i][j+(1<< (i1-1))][i1-1], Minm[i+(1<< (i1-1))][j+ (1<< (i1-1))][i1-1] ) ); } } Long LongAns= (1<< to); Ans*=ans; for(intI=1; i+n-1<=a;i++) for(intj=1; j+n-1<=b;j++) { intls=int(log2 (n)); intMaxnumber=Max (Max (maxm[i][j][ls), Maxm[i+n-(1<<ls)] [j] [LS]), max (Maxm[i][j+n-(1<<ls)] [LS], maxm[i+n-(1<<LS)][j+n-(1<<ls)] [LS]) ); intMinnumber=min (min (minm[i][j][ls), Minm[i+n-(1<<ls)] [j] [LS]), min (minm[i][j+n-(1<<ls)] [LS], minm[i+n-(1<<LS)][j+n-(1<<ls)] [LS]) ); if(maxnumber-minnumber<ans) ans=maxnumber-Minnumber; } cout<<ans<<Endl; return 0;}
[BZOJ1047] [HAOI2007] the ideal square