codeforces 375B B. Maximum Submatrix 2(dp)

题目链接:

codeforces 375B


题目大意:

给出一个01矩阵,行与行之间可以互换位置,问能够得到最大的全1矩阵的面积。


题目分析:

  • 我们有一种模型,就是对于不互换行的01矩阵求最大面积,就是固定子矩阵的右下角,记录h[i][j]就是当前位置的高度,然后向左延展的距离,枚举所有的即可。
  • 代码如下:
#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 );
    }
}
  • 那么对于可以行互换的,我们已经记录每个点能够向左延展的距离,定义状态dp[j][i]代表第j列的第i行的元素向左延展的最远距离,然后我们对这个距离排序,从大到小,每个上面的都可以被下面的拓展,那么依旧是枚举每个右下角,取最大的结果。

AC代码:

#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 );
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-06 00:23:01

codeforces 375B B. Maximum Submatrix 2(dp)的相关文章

CodeForces B. The least round way(dp)

题目链接:http://codeforces.com/problemset/problem/2/B B. The least round way time limit per test 5 seconds memory limit per test 64 megabytes input standard input output standard output There is a square matrix n?×?n, consisting of non-negative integer n

Codeforces Round #260 (Div. 1) A. Boredom (DP)

题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Alex doesn't like boredom. That's why whenever he gets bored, he comes up with

hdu4939 Stupid Tower Defense (DP)

2014多校7 第二水的题 4939 Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 366    Accepted Submission(s): 88 Problem Description FSF is addicted to a stupid tower defense game.

HDU - 4971 A simple brute force problem. (DP)

Problem Description There's a company with several projects to be done. Finish a project will get you profits. However, there are some technical problems for some specific projects. To solve the problem, the manager will train his employee which may

Codeforces118D Caesar&#39;s Legions(DP)

题目 Source http://codeforces.com/problemset/problem/118/D Description Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had n1 footmen and n2 horsemen. Caesar thought that an arrangement is not beautiful if somewhe

ZOJ 1642 Match for Bonus(dp)

Match for Bonus Time Limit: 2 Seconds      Memory Limit: 65536 KB Roy played a game with his roommates the other day. His roommates wrote 2 strings of characters, and gave each character a bonus value. When Roy pinned the positions of one common char

【HDOJ 5653】 Bomber Man wants to bomb an Array.(DP)

[HDOJ 5653] Bomber Man wants to bomb an Array.(DP) Bomber Man wants to bomb an Array. Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 389    Accepted Submission(s): 117 Problem Description Give

【POJ 2029】 Get Many Persimmon Trees(DP)

[POJ 2029] Get Many Persimmon Trees(DP) Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4024   Accepted: 2628 Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18

【POJ 3034】 Whac-a-Mole(DP)

[POJ 3034] Whac-a-Mole(DP) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3621   Accepted: 1070 Description While visiting a traveling fun fair you suddenly have an urge to break the high score in the Whac-a-Mole game. The goal of the W