zoj 2180 - City Game

题目:给你一个1000*1000的01矩阵,求里面全是1的最大矩形面积。

分析:dp,单调队列。zju1985升级版 ^_^ 继上次那道题想了一天。

本来想用O(N^2)的最大正方形求解,想错了今天仔细一看,其实这道题目就是二维的最大矩形;

我们将问题分解成最大矩形,即求解以k行为底边的图形中的最大矩形,然后合并,求最大的矩形;

预处理: 求出以每行为底边的每一列从底边开始向上的最大连续1的高度MaxH。 O(N^2) ;

DP:对于每一层底边,我们利用单调队列求解出本行的最大矩形。 O(N);

关于单调队列的求解分析,可参照zoj1985的题解;

总体时间:T(N) = O(N^2)+O(N)*O(N) = O(N^2)。

说明: (2011-09-19 01:36)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char Maps[ 1003 ][ 1003 ];
int  MaxH[ 1003 ][ 1003 ];
int  L[ 1003 ],R[ 1003 ];
int  MUQ[ 1003 ];

int main()
{
    int t,m,n;
    while ( scanf("%d",&t) != EOF )
    while ( t -- ) {
        scanf("%d%d",&m,&n);getchar();
        for ( int i = 1 ; i <= m ; ++ i )
        for ( int j = 1 ; j <= n ; ++ j ) {
            scanf("%c",&Maps[ i ][ j ]);
            getchar();
        }

        //计算每条底边上的每列高度
        memset( MaxH, 0, sizeof( MaxH ) );
        for ( int i = 1 ; i <= m ; ++ i )
        for ( int j = 1 ; j <= n ; ++ j )
            if ( Maps[ i ][ j ] == 'F' )
                MaxH[ i ][ j ] = MaxH[ i-1 ][ j ]+1;
            else
                MaxH[ i ][ j ] = 0;

        for ( int i = 1 ; i <= m ; ++ i )
            MaxH[ i ][ 0 ] = MaxH[ i ][ n+1 ] = -1;

        int MaxV = 0;
        for ( int i = 1 ; i <= m ; ++ i ) {
            //计算每个点的左边界
            int tail = 0;
            MUQ[ 0 ] = 0;
            for ( int j = 1 ; j <= n+1 ; ++ j ) {
                while ( tail >= 0 && MaxH[ i ][ MUQ[ tail ] ] > MaxH[ i ][ j ] )
                        R[ MUQ[ tail -- ] ] = j;
                MUQ[ ++ tail ] = j;
            }
            //计算每个点的右边界
                tail = 0;
            MUQ[ 0 ] = n+1;
            for ( int j = n ; j >= 0 ; -- j ) {
                while ( tail >= 0 && MaxH[ i ][ MUQ[ tail ] ] > MaxH[ i ][ j ] )
                    L[ MUQ[ tail -- ] ] = j;
                MUQ[ ++ tail ] = j;
            }
            //求解
            for ( int j = 1 ; j <= n ; ++ j ) {
                int Temp = MaxH[ i ][ j ]*(R[ j ]-L[ j ]-1);
                if ( MaxV < Temp )
                    MaxV = Temp;
            }
        }

        printf("%d\n",MaxV*3);
    }
    return 0;
}
时间: 2024-11-05 12:53:42

zoj 2180 - City Game的相关文章

ZOJ 3041 City Selection(好排序)

题目链接:ZOJ 3041 City Selection 题意:有N个城市坐标和M个工厂坐标,在以工厂为原点的第四象限的点都会受到污染.即图中画线区域 思路:把工厂和城市的坐标一起排序,再比较y坐标. AC代码: #include <stdio.h> #include <algorithm> #include <set> using namespace std; const int maxn=200010; struct node{ int x,y; int flag;

ZOJ 3042 City Selection II 【序】【离散化】【数学】

题意: 输入数据n,m.n代表工厂的数量,m代表城市的数量. 接下来n+m行为工厂和城市的坐标. 规定如图所示方向刮风,工厂的air会污染风向地区的air. 注意,工厂和城市的坐标表示的是从x到x+1从y到y+1之间小正方形都是工厂区域,规定如果只有一个coner的air被污染那么该地区视为无污染. 要求输出有多少不被污染的城市. 坑: 思考了很多问题.... 1.加入某城市的正下方是工厂怎么办...这个是否算污染. 2.假如有两个角被污染了怎么办...算污染吗. 坑了一整天.没办法找大神的代码

zoj 3041 City Selection(数学啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2039 The government decided to build some cities in a newly developping area. Now they had N different locations to select from, while there were M factories in this newly developping ar

ZOJ 3211 Dream City (J) DP

Dream City Time Limit: 1 Second      Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the yard. Let's call them tree 1, tree 2 ...and tree n. At the first day, each tree i has ai coins

ZOJ 3195 Design the city

倍增法在线LCA..... ZOJ Problem Set - 3195 Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now,

ZOJ Design the city LCA转RMQ

Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason

ZOJ Problem Set - 3195 Design the city 【Tarjan离线LCA】

题目:ZOJ Problem Set - 3195 Design the city 题意:给出一个图,求三点的连起来的距离. 分析:分别求出三点中任意两点的距离 / 2  = ans AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; #define N 50010 #define M 20010 struc

ZOJ 3211 Dream City(线性DP)

Dream City Time Limit: 1 Second      Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the yard. Let's call them tree 1, tree 2 ...and tree n. At the first day, each tree i has aicoins o

zoj 3211 - Dream City

题目:javaman来到了一个城市,这里有很多长着金币的树,每棵树每晚还会结出新的金币, 现在他每天白天只能砍一棵树,最多在这里呆m天,求能得到的最大金币数. 分析:贪心+dp,二维01背包.如果砍树的集合确定,那一定按照b递增的顺序砍,因此排序后背包. 说明:(2011-11-02 05:49). #include <stdio.h> #include <stdlib.h> #include <string.h> #define max(a,b) ((a)>(b