Codeforces Round #221 (Div. 2) D. Maximum Submatrix 2 (thinking question ),
Question address: codeforces 221 D
This is the first question of D in CF in our life. (At that time, only question A was made ..) When I reviewed the Chinese New Year, I took a few D questions by the way. Currently, the question "D" of CF cannot be done during the competition. However, you can do it yourself after the game. It is said that if question D can stabilize in the competition, it will be able to compete in the regional silver. So we strive for four questions in the future.
This question should not be labeled at the beginning .. It is DP .. So I keep thinking in the DP direction. But it does not feel like DP. I changed my mind and made it.
The general method is to pre-process the maximum number of consecutive 1 Numbers extending to the left of each row. Then sort each row (hash is used here, and the speed is faster). The current area is calculated from the largest to the smallest and the maximum value is obtained.
The Code is as follows:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")#define LL __int64#define pi acos(-1.0)const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;int dp[5002], mark[5002][5002];char s[5002];int main(){ int n, m, i, j, max1, tmp; while(scanf("%d%d",&n,&m)!=EOF){ memset(mark,0,sizeof(mark)); getchar(); for(i=0;i<n;i++){ gets(s); dp[0]=0; for(j=1;j<=m;j++){ if(s[j-1]=='1') { dp[j]=dp[j-1]+1; mark[j][dp[j]]++; } else dp[j]=0; } } max1=0; for(j=1;j<=m;j++){ tmp=0; for(i=m;i>=1;i--){ if(!mark[j][i]) continue ; tmp+=mark[j][i]; max1=max(max1,tmp*i); } } printf("%d\n",max1); } return 0;}