Sgu-250 Constructive Plan
Question:
Here is N? M Of {0, 1} Matrix, and then only 0 And then you need to find the largest C , The size of the output, and change the occupied position in the source image 8 And then output a new graph.
PS: C Is defined as three adjacent sizes from top to bottom and not 0 The left boundary must be aligned, and the right boundary of the top and bottom rectangles must be greater than the right boundary of the middle rectangle.
Solution:
I just came up with this question. O (N3logN) Although I can, I wrote half of them wrong, And then I accidentally saw the code of zookeeper. 3 Cycles, "slot, N3 So I immediately went to worship ".
O (N3logN) I will not repeat the algorithm here. I can find it on the Internet. O (N3) .
First:
We use F [I] [l] [r] Indicates I Row, number L Column and Column L + r? 1 Column, which is the maximum area of the rectangle at the bottom boundary.
Second:
Then we make G [I] [l] [r] Indicates that the rectangle in the middle is I Row, number L Column and Column L + r? 1 Column, which is the maximum area and of the first two rectangles at the bottom boundary.
Obviously G [I] [l] [r] The transfer equation is I Row L Column to L + r? 1 All columns are 0 It is the following formula; otherwise, it is 0 )
G [I] [l] [r] = max (G [I? 1] [l] [r] + r, r + max (F [I? 1] [l] [k], k> r ))
Apparently Max (F [I? 1] [l] [k], k> r) You can record updates with a variable to achieve O (1)
Thrid:
Ling H [I] [l] [r] Indicates that the bottom rectangle is I Row, number L Column and Column L + r? 1 Column, which is the maximum answer for the lower boundary.
The same G Calculation is similar, H [I] [l] [r] The transfer equation is I Row L Column to L + r? 1 All columns are 0 It is the following formula; otherwise, it is 0 )
H [I] [l] [r] = max (H [I? 1] [l] [r] + r, r + max (G [I? 1] [l] [k], k
Then the problem is solved.
The output is simple.
AC code:
Note: I am too weak to learn the problem while reading Zookeeper's code. Then I accidentally wrote the problem similar to zookeeper. QAQ
#include
#include
#include
#include
#include #include
using namespace std;int n,m;int ch[200][200]={{0}};short top[200][200][200]={{{0}}};short F[200][200][200]={{{0}}};short G[200][200][200]={{{0}}};short H[200][200][200]={{{0}}};int area=0;int ai=0,al=0,ar=0;int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf("%d",&ch[i][j]); top[i][j][1]=(short)(ch[i][j]^1)*(top[i-1][j][1]+1); } for(int i=n;i>=1;i--) for(int j=m;j>=1;j--) { if(ch[i][j]==1) continue; for(int k=2;k+j-1<=m && ch[i][k+j-1]==0;k++) { top[i][j][k]=min(top[i][j][1],top[i][j+1][k-1]); F[i][j][k]=top[i][j][k]*k; } } for(int i=2;i<=n;i++) for(int j=m;j>=1;j--) { if(ch[i][j]==1) continue; int k; for(k=1;k+j-1<=m && ch[i][k+j-1]==0;k++);k--; int Max=0; for(int r=m-j+1;r>k;r--) Max=max((int)F[i-1][j][r],Max); for(int r=k;r>=1;r--) { if(G[i-1][j][r]>0) G[i][j][r]=G[i-1][j][r]+r; if(Max>0) G[i][j][r]=max((short)(Max+r),G[i][j][r]); Max=max((int)F[i-1][j][r],Max); } } for(int i=2;i<=n;i++) for(int j=m;j>=1;j--) { if(ch[i][j]==1) continue; int Max=0; for(int k=1;k+j-1<=m && ch[i][k+j-1]==0;k++) { H[i][j][k]=max((H[i-1][j][k]>0)*(H[i-1][j][k]+k),(Max>0)*(Max+k)); Max=max((int)G[i-1][j][k],Max); if(H[i][j][k]>area) { area=H[i][j][k]; ai=i,al=j,ar=k; } } } if(area<5) cout<<-1<
0;ai--) for(int i=1;i<=ar;i++) ch[ai][al+i-1]=8; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) printf("%d ",ch[i][j]); puts(""); } } return 0;}