标签:
Time Limit: 20 Sec
Memory Limit: 256 MB
http://codeforces.com/problemset/problem/375/B
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 5000). Next n lines contain m characters each — matrix a. Matrix a only contains characters: "0" and "1". Note that the elements of the matrix follow without any spaces in the lines.
Output
Print a single integer — the area of the maximum obtained submatrix. If we cannot obtain a matrix of numbers one, print 0.
Sample Input
1 1
1
Sample Output
1
题意
给你一个01矩阵,行与行之间可以交换位置
然后问你构成构成最大的只含1的矩形的面积是多少
题解:
我们分析一下,首先我们预处理一下
dp[i][j]表示第i列第j行往左边最远能延长多远
因为列是不会变的,所以我们对于每一列都排序,然后利用dp的思想往下找
到dp[i][j]==0的时候break,因为显然剩下的都是0了
代码
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) #define maxn 5005 #define mod 10007 #define eps 1e-5 const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } //************************************************************************************** char s[maxn][maxn]; int dp[maxn][maxn]; bool cmp(int a,int b) { return a>b; } int main() { int n=read(),m=read(); int ans=0; for(int i=1;i<=n;i++) scanf("%s",s[i]+1); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(s[i][j]==‘1‘) dp[j][i]=dp[j-1][i]+1; for(int i=1;i<=m;i++) { sort(dp[i]+1,dp[i]+1+n,cmp); for(int j=1;j<=n;j++) { if(dp[i][j]==0) break; ans=max(dp[i][j]*j,ans); } } cout<<ans<<endl; }
Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序
标签:
原文地址:http://www.cnblogs.com/qscqesze/p/4607185.html