P1719 Maximum Weighted rectangle and P1719 weighted rectangle
In order to better prepare for NOIP2013, several girls in the computer group, LYQ, ZSC, and ZHQ, believe that we not only need data centers, but also sports, so I decided to ask the principal to apply for a computer group's after-school sports venue. I heard that they are all masters of the computer group. The principal did not immediately promise them, but first gave them a math question and told them: the area of the sports ground you can obtain is the largest number you can find.
The principal first gives them an N * N matrix. The maximum weighted rectangle in the matrix is required, that is, each element of the matrix has a weight value, which is defined in the Integer Set. Find a rectangle from it. The rectangle size is unlimited, which is the sum of all elements in it. Each element of the matrix belongs to [-127,127], for example
0-2-7 0 in the lower left corner: 9 2
9 2-6 2-4 1
-4 1-4 1-1 8
-1 8 0-2 and 15
A few girls were a little hard, so they found the computer group to calculate the HZH, TZY children help calculate, but unfortunately their answers are different, we can not be vague about land, can you help us calculate the largest and weighted rectangle given by the principal?
Input/Output Format
Input Format:
The first row: n, followed by the matrix of n rows and n columns.
Output Format:
The sum of the largest rectangle (Child matrix.
Input and Output sample input sample #1:
40 –2 –7 0 9 2 –6 2-4 1 –4 1 –1 8 0 –2
Output sample #1:
15
Description
N <= 120
This question is timed out in four dimensions,
So we have reduced the maintenance.
Sum [I] [j] indicates the sum of the number of j before column I.
Then we can calculate the maximum child segment sum every time.
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 void read(int &n) 7 { 8 char c='+';int x=0;bool flag=0; 9 while(c<'0'||c>'9')10 {c=getchar();if(c=='-')flag=1;}11 while(c>='0'&&c<='9')12 {x=x*10+(c-48);c=getchar();}13 flag==1?n=-x:n=x;14 }15 int n;16 int map[200][200];17 int sum[200][200];18 int c[200];19 int ans=-0x77777f;20 int main()21 {22 read(n);23 for(int i=1;i<=n;i++)24 for(int j=1;j<=n;j++)25 {26 read(map[i][j]);27 sum[j][i]=sum[j][i-1]+map[i][j];28 }29 for(int i=1;i<=n;i++)30 for(int j=i;j<=n;j++)31 for(int k=1;k<=n;k++)32 {33 c[k]=max(sum[k][j]-sum[k][i-1],c[k-1]+sum[k][j]-sum[k][i-1]);34 ans=max(ans,c[k]); 35 } 36 printf("%d",ans);37 }