Codeforces Round #221 (Div. 2) D. Maximum Submatrix 2 (思维题)

来源:互联网 发布:朝鲜有多穷 知乎 编辑:程序博客网 时间:2024/05/24 04:10

题目地址:codeforces 221 D
这场是人生中做的第一场CF中的D题。(当时只做出来了A题。。)过年之际回顾了一下,就顺便看了几道D题。现在做CF的D题在比赛时还是做不出来。但是赛后往往都可以自己做出来。据说D题能在比赛中稳出的话就可以区域赛银了。于是争取以后CF能稳出4道题吧。
这道题刚开始不该看标签的。。给的是DP。。于是就一直朝着DP方向想。但是感觉不像是DP。就换了个思路,就做出来了。
大体方法是先预处理出每一行中每个数向左延伸最长的连续1的个数。然后对每一行的进行排序(我这里用的是hash,速度更快),从大到小开始不断算出当前面积并取最大值。
代码如下:

#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;}
1 0
原创粉丝点击