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

题目地址: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;
}
时间: 2024-12-25 22:27:36

Codeforces Round #221 (Div. 2) D. Maximum Submatrix 2 (思维题)的相关文章

Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序

B. Maximum Submatrix 2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/375/B Description You are given a matrix consisting of digits zero and one, its size is n × m. You are allowed to rearrange its rows. What is

Codeforces Round #298 (Div. 2) A、B、C题

题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side

Codeforces Round #172 (Div. 2)---D. Maximum Xor Secondary(RMQ + 二分)

Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1,?x2,?-,?xk (k?>?1) is such maximum element xj, that the following inequality holds: . The lucky number of the sequenc

Codeforces Round #599 (Div. 2) A. Maximum Square

Ujan decided to make a new wooden roof for the house. He has nn rectangular planks numbered from 11 to nn. The ii-th plank has size ai×1ai×1 (that is, the width is 11 and the height is aiai). Now, Ujan wants to make a square roof. He will first choos

Codeforces Round #221 (Div. 2) D

有点郁闷的题目,给了2000ms,可是n,m的范围已经是5000了.5000 * 5000一般在别的OJ已经是超了2000ms,一開始不敢敲.看了下别人有n*m的潜逃循环,原来CF的机子如此的强大,一開始题意没看清错了,原来随意行能够交换,列不行 那就先dp出 每一行的 每个位置包含它本身以及前面的连续出现1的长度.然后再对列进行处理.由于列是不能变的.所以相应列是固定的,那么就对列枚举,然后由于行能够交换.所以详细哪一列在哪一行能够变化.就把前面dp出的最大的放在最后面,意思就是呈现一个倒着放

Codeforces Round #276 (Div. 2)D - Maximum Value(筛法)

就是一种筛法思想的应用. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm> #include<

Codeforces Round #337 (Div. 2) B. Vika and Squares 水题

B. Vika and Squares Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains ai liters of paint of color i. Vika also has an infinitely long rectangular piece of paper of width 1, consisting of s

Codeforces Round #296 (Div. 2) C. Glass Carving(想法题)

传送门 Description Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular wmm  ×  h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understa

Codeforces Round #342 (Div. 2) C. K-special Tables(想法题)

传送门 Description People do many crazy things to stand out in a crowd. Some of them dance, some learn by heart rules of Russian language, some try to become an outstanding competitive programmers, while others collect funny math objects. Alis is among