[HAOI2007] the ideal square

Source: Internet
Author: User

1047: [HAOI2007] Ideal square Time limit:10 Sec Memory limit:162 MB
submit:1972 solved:1043
[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  [Submit] [Status] [Discuss]

"A brief analysis of the algorithm"

The size of the problem is quite large, and theO (ABN) algorithm will time out, so only the O (ablogn) or o (AB) algorithm can be considered .

The following is an O (AB) algorithm: Using a monotone queue.

Enumeration in the subject is essential, and the worst can beO (AB), so it must be for thisO (AB)enumeration to calculate some states, that is, the(I,J)to the upper-left corner of theNxNthe maximum and minimum values in the square area. The problem of this two-dimensional simplification into one dimension, is to(I,J)the length of the left isNthe maximum value of the sequencemax[I,J]and minimum valuemin[I,J], and then the same method can be used to calculate the two-dimensional state by this one-dimensional state. And now our mission is toO (b)time to calculate a row in themax[Iand themin[I. This requires the use of a monotonous queue.

Open two queues, one to maintain the maximum, and one to maintain the minimum value. To facilitate the narrative, only the largest teams are discussed below, and the minimum team is maintained in a similar manner.

We want to make sure that the size of each element in the queue is monotonically decreasing (note that it is not monotonically rising) and that the subscript of each element is monotonically increasing. This will ensure that the first element of the team is the largest, and that the team head is always the biggest at the moment of renewal. Therefore, it is necessary to change the queue so that it can be deleted at both ends and inserted at the end of the team.

In order to ensure monotonic, each time the insertion, the first judgment of the team tail elements, if not more than the inserted element to delete, and constantly delete the end of the team until the tail element is larger than the element to be inserted or the team empty. Delete, judge the team first, if the team first element subscript is less than the current segment left edge Delete, constantly delete the team head until the first element of the line is greater than equal to the current segment of the left edge (note: The queue is certainly not empty), the first element of the team is the current segment of the optimal solution.

With a monotone queue, computing max[i] and min[i] is convenient. At the beginning, the n-1 elements of the current line are inserted at the end of the queue. Starting from column 1, each time the team first optimal value is removed, the invalid element is deleted from the first team and the current column +n-1 element is inserted into the tail of the team. The time complexity is O (b), since each element is enqueued at most .

The optimal solution of the square can be calculated in O (a) time according to max[i] and min[i] in the same way , and the time complexity is O (ab ).

1#include <iostream>2#include <cstring>3#include <cstdlib>4#include <cstdio>5#include <algorithm>6 #defineN 11117 8 using namespacestd;9 Ten Long LongA,b,n;//Ìâä¿òªçó,aîªðð,bîªáð£¬nîªòªçóµäõý ½ðî±ß³¤ One Long LongMap[n][n];//´æí¼ A Long LongQ[n][n];//¶óáð - Long LongH[n],t[n];//¶óêx£¬¶óî²ö¸õë - Long LongS,e,dq[n]; the Long LongMx[n][n],ans,mn[n][n]; -  -InlinevoidGetmax () - { +      for(Long LongI=1; i<=b;i++) h[i]=1, t[i]=0;//³õê¼»¯£¬í Ö¸õëîª1£¬î²ö¸õëîª0 -      +      for(Long LongI=1; i<=a;i++)//ö¾ùðð A     { at          for(Long Longj=1; j<=b;j++)//ö¾ùáð -         { -              -              while(h[j]<=t[j]&&i-q[j][h[j]]+1>n)//Èô¶óáð²»îª¿õçò³¬¹ýnµäïþöæ -h[j]++;//ɾ³ý¶óêxôªëø -                   in              while(H[j]<=t[j]&&map[q[j][t[j]]][j]<=map[i][j])//Èô¶óáð²»îª¿õçòòª²åèëµäôªëø´ó -t[j]--;//Óú¶óî²ôªëø to              +Q[j][++t[j]]=i;//²åèë´ëôªëø -         } theE=0;//βö¸õë *s=1;//í Ö¸õë $          for(Long Longj=1; j<=b;j++)//ö¾ùáðPanax Notoginseng         { -              while(s<=e&&j-dq[s]+1>N) thes++; +              while(s<=e&&map[q[dq[e]][h[dq[e]]]][dq[e]]<=Map[q[j][h[j]]][j]) Ae--; thedq[++e]=J; +mx[i][j]=Map[q[dq[s]][h[dq[s]]]][dq[s]]; -         } $     } $ } -  -Inlinevoidgetmin () the { -      for(Long LongI=1; i<=b;i++) h[i]=1, t[i]=0;Wuyi      for(Long LongI=1; i<=a;i++) the     { -          for(Long Longj=1; j<=b;j++) Wu         { -              while(h[j]<=t[j]&&i-q[j][h[j]]+1>N) Abouth[j]++; $              while(h[j]<=t[j]&&map[q[j][t[j]]][j]>=Map[i][j]) -t[j]--; -q[j][++t[j]]=i;  -         } AE=0; s=1; +          for(Long Longj=1; j<=b;j++) the         { -              while(s<=e&&j-dq[s]+1>N) $s++; the              while(s<=e&&map[q[dq[e]][h[dq[e]]]][dq[e]]>=Map[q[j][h[j]]][j]) thee--; thedq[++e]=J; themn[i][j]=Map[q[dq[s]][h[dq[s]]]][dq[s]]; -         } in     } the } the  About intMain () the { thescanf"%lld%d%d",&a,&b,&n); the       for(Long LongI=1; i<=a;i++) +          for(Long Longj=1; j<=b;j++) -scanf"%lld",&map[i][j]); the  Bayi   the getmin (); the Getmax (); -ans=1ll<< -; -       for(Long Longi=n;i<=a;i++) the          for(Long Longj=n;j<=b;j++) theAns=min (ans,mx[i][j]-mn[i][j]); theprintf"%lld\n", ans); the         return 0; -}

[HAOI2007] the 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.