Cattle off more training school second field H. Second Large Rectangle (prefix + and monotonous stack)

Topic Portal

The meaning of problems

Input integers n and m (n and m are ∈ [1,1000]), gives an n × m matrix 01, seeking a second area Encyclopedia matrix. (If less than two matrix 1, and outputs 0)

answer

Monotone stack approach: first preprocessing each point (i, j) is high up [i] [j], each row as enumerated base, through each point j, calculates the current stack maintains a monotonically decreasing height up to a maximum 1 the whole area of ​​rectangular section (1 matrix full width i.e. the maximum current that can be formed), to give the current width × height may be formed at this time if the largest area and the recording the optimal solution can be obtained to keep the maximum full matrix 1 area.

However, requirements of the subject is the second largest, so that the present Tacca naive as long as the output of the second largest area of all the records into an array, arranged for the sequence after partial_sort OK. too young too simple ~

Having gone through again and again to check the next WA, saying that there will be the second largest in the matrix contains only the largest matrix inside the case, but since the construction could not meet the test cases to verify the situation so he did not understand. Eventually he went to write a random test generation, to take a shot at AC code and found the following error in two test cases.

6 2 24-6
100101, China correct answer: 5 101010001011100001001101 correct answer: 5
011011 My answer: 4 110111111001001100010101 My answer: 4
111111
010100
011010
001110

It requires a large matrix of the second area, in addition to the operator area width × height, but also more in both cases :( width count -1) × height, width × (high -1).

Code

/*133ms*/
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1005; int a[maxn][maxn],up[maxn][maxn],ans[3*maxn*maxn];//注意这里要3*maxn*maxn int main() { int n,m; while(~scanf("%d%d",&n,&m)){ memset(up,0,sizeof(up)); memset(a,0,sizeof(a)); memset(ans,0,sizeof(ans)); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%1d",&a[i][j]); up[i][j]=(a[i][j]==1)?up[i-1][j]+1:0; } } int area,q=0; for(int i=1;i<=n;i++){ stack<pair<int,int> >S; for(int j=1;j<=m+1;j++){ int L=j; while(!S.empty()&&up[i][j]<=S.top().first){ L=S.top().second; area=(j-L)*S.top().first; ans[q++]=area; area=(j-L-1)*S.top().first; ans[q++]=area; area=(j-L)*(S.top().first-1); ans[q++]=area; S.pop(); } if(S.empty()||up[i][j]>S.top().first) S.push({up[i][j],L}); } } partial_sort(ans,ans+2,ans+q,greater<int>()); cout<<ans[1]<<endl; } return 0; }

 




Guess you like

Origin www.cnblogs.com/HOLLAY/p/11355539.html