标签:codeforces dp
给出一个01矩阵,行与行之间可以互换位置,问能够得到最大的全1矩阵的面积。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAX 5007
using namespace std;
int n,m;
char mp[MAX][MAX];
int lef[MAX];
int h[MAX][MAX];
int main ( )
{
while ( ~scanf ( "%d%d" , &n , &m ))
{
for ( int i = 1 ; i <= n ; i++ )
scanf ( "%s" , mp[i]+1 );
memset ( h , 0 , sizeof ( h ));
int ans = 0;
for ( int i = 1 ; i <= n ; i++ )
{
for ( int j = 1 ; j <= m ; j++ )
{
if ( mp[i][j] == ‘0‘ ) h[i][j] = 0;
else if ( i == 1 || mp[i-1][j]==‘0‘)
h[i][j] = 1;
else h[i][j] = h[i-1][j]+1;
if ( j == 1 || h[i][j-1] < h[i][j] )
lef[j] = j;
else lef[j] = lef[j-1];
}
for ( int j = 1 ; j <= m ; j++ )
ans = max ( ans , (j-lef[j]+1)*h[i][j] );
}
printf ( "%d\n" , ans );
}
}
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAX 5007
using namespace std;
int n,m;
int dp[MAX][MAX];
char mp[MAX][MAX];
int main ()
{
while ( ~scanf ( "%d%d" , &n , &m ))
{
for ( int i = 1 ; i<= n ; i++ )
scanf ( "%s" , mp[i]+1 );
for ( int i= 1 ; i <= n ; i++ )
for ( int j = 1 ; j <= m ; j++ )
if ( mp[i][j] == ‘1‘ )
dp[j][i] = dp[j-1][i]+1;
else dp[j][i] = 0;
int ans = 0;
for ( int i = 1 ; i <= m ; i++ )
{
sort ( dp[i]+1 , dp[i]+n+1 );
for ( int j = n ; j > 0 ; j-- )
ans = max ( ans , dp[i][j]*(n-j+1) );
}
printf ( "%d\n" , ans );
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
codeforces 375B B. Maximum Submatrix 2(dp)
标签:codeforces dp
原文地址:http://blog.csdn.net/qq_24451605/article/details/48651451