Maximum sub-matrix
Time limit:30000/10000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3906 Accepted Submission (s): 1994
Problem description gives you an integer matrix of MXN, looking up a XXY sub-matrix that makes the and largest of all the elements in the sub-matrix.
The first behavior of input data is a positive integer t, which indicates that there is a T group of test data. The first behavior of each set of test data is four positive integer m,n,x,y (0<m,n<1000 and 0<x<=m and 0<y<=n), indicating that the given rectangle has m row n columns. Next, the matrix has m lines, each line having n a positive integer not greater than 1000.
Output for each set of data, outputs an integer that represents the maximum and of the sub-matrices.
Sample Input14 5 2 23 361 649 676 588992 762 156 993 169662 34 638 89 543525 165 254 809 280
Sample Output2474
Authorlwg
Source HDU 2006-12 Programming Contest Many methods, two-dimensional tree-like array, DP, two-dimensional rmq ... Data is very water, two-dimensional tree array 491MS, there is time to complement the two-dimensional RMQ,DP solution
#include <stdio.h>#include<algorithm>#include<string.h>#include<iostream>#defineN 1005using namespacestd;intC[n][n];intn,m,x,y;intLowbit (intx1) { returnx1& (-x1);}intUpdateintX1,intY1,intvalue) { for(inti=x1;i<=n;i+=lowbit (i)) { for(intj=y1;j<=m;j+=Lowbit (j)) {C[i][j]+=value; } }}intGetsum (intX1,inty1) { intValue =0; for(inti=x1;i>=1; i-=lowbit (i)) { for(intj=y1;j>=1; j-=Lowbit (j)) {Value+=C[i][j]; } } returnvalue;}intMain () {inttcase; scanf ("%d",&tcase); while(tcase--) {memset (C,0,sizeof(C)); scanf ("%d%d%d%d",&n,&m,&x,&y); intnum; for(intI=1; i<=n;i++){ for(intj=1; j<=m;j++) {scanf ("%d",&num); Update (I,j,num); } } intMX =-1; for(intI=1; i<=n;i++){ if(i+x-1>n)Continue; for(intj=1; j<=m;j++){ if(j+y-1>M)Continue; intX1 =i; intx2 = i+x-1; intY1 =J; inty2 = j+y-1; intsum = Getsum (x2,y2)-getsum (x1-1, y2)-getsum (x2,y1-1) +getsum (x1-1, y1-1); //printf ("%d\n", sum); if(sum>mx) MX =sum; }} printf ("%d\n", MX); } return 0;}
HDU 1559 (maximum sub-matrix)