hdu1505---City Game(单调栈)

Problem Description

Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees,factories and buildings. There is still some space in the area that is unoccupied. The strategic task of his game is to win as much rent money from these free spaces. To win rent money you must erect buildings, that can only be rectangular, as long and wide as you can. Bob is trying to find a way to build the biggest possible building in each area. But he comes across some problems – he is not allowed to destroy already existing buildings, trees, factories and streets in the area he is building in.

Each area has its width and length. The area is divided into a grid of equal square units.The rent paid for each unit on which you’re building stands is 3$.

Your task is to help Bob solve this problem. The whole city is divided into K areas. Each one of the areas is rectangular and has a different grid size with its own length M and width N.The existing occupied units are marked with the symbol R. The unoccupied units are marked with the symbol F.

Input

The first line of the input contains an integer K – determining the number of datasets. Next lines contain the area descriptions. One description is defined in the following way: The first line contains two integers-area length M<=1000 and width N<=1000, separated by a blank space. The next M lines contain N symbols that mark the reserved or free grid units,separated by a blank space. The symbols used are:

R – reserved unit

F – free unit

In the end of each area description there is a separating line.

Output

For each data set in the input print on a separate line, on the standard output, the integer that represents the profit obtained by erecting the largest building in the area encoded by the data set.

Sample Input

2

5 6

R F F F F F

F F F F F F

R R R F F F

F F F F F F

F F F F F F

5 5

R R R R R

R R R R R

R R R R R

R R R R R

R R R R R

Sample Output

45

0

Source

Southeastern Europe 2004

单调栈随便搞,枚举行,然后往左往右扩展

/*************************************************************************
    > File Name: hdu1505.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年05月10日 星期日 21时43分44秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 2010;
char mat[N][N];
char str[10];
int height[N][N];
PLL Stack[N];
int Top;
int L[N];
int R[N];
int use[N][N];

int main() {
    int n, m;
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                scanf("%s", str);
                mat[i][j] = str[0];
            }
        }
        for (int i = 1; i <= m; ++i) {
            height[i][0] = 0;
            for (int j = 1; j <= n; ++j) {
                height[i][j] = height[i][j - 1];
                if (mat[j][i] == ‘F‘) {
                    ++height[i][j];
                }
                else {
                    height[i][j] = 0;
                }
            }
        }
        memset(use, 0, sizeof(use));
        int ans = 0;
        for (int i = 1; i <= n; ++i) {
            Top = 0;
            for (int j = m; j >= 1; --j) {
                if (!Top) {
                    Stack[++Top] = make_pair(height[j][i], j);
                }
                else {
                    while (Top) {
                        PLL u = Stack[Top];
                        if (u.first <= height[j][i]) {
                            break;
                        }
                        L[u.second] = j + 1;
                        --Top;
                    }
                    Stack[++Top] = make_pair(height[j][i], j);
                }
            }
            while (Top) {
                PLL u = Stack[Top];
                --Top;
                L[u.second] = 1;
            }
            for (int j = 1; j <= m; ++j) {
                if (!Top) {
                    Stack[++Top] = make_pair(height[j][i], j);
                }
                else {
                    while (Top) {
                        PLL u = Stack[Top];
                        if (u.first <= height[j][i]) {
                            break;
                        }
                        R[u.second] = j - 1;
                        --Top;
                    }
                    Stack[++Top] = make_pair(height[j][i], j);
                }
            }
            while (Top) {
                PLL u = Stack[Top];
                --Top;
                R[u.second] = m;
            }
            for (int j = 1; j <= m; ++j) {
                int l = L[j];
                int r = R[j];
                ans = max(ans, (r - l + 1) * height[j][i]);
            }
        }
        printf("%d\n", ans * 3);
    }
    return 0;
}
时间: 2024-10-12 16:40:33

hdu1505---City Game(单调栈)的相关文章

HDOJ 4252 A Famous City 单调栈

单调栈: 维护一个单调栈 A Famous City Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1671    Accepted Submission(s): 644 Problem Description After Mr. B arrived in Warsaw, he was shocked by the skyscrap

[USACO2005][POJ3044]City Skyline(贪心+单调栈)

题目:http://poj.org/problem?id=3044 题意:以坐标的形式给出一张图,表示一些楼房的正视图,求出楼房的最少个数. 分析:和小学常做的立方体问题很像,很容易想到一个贪心方法,那就是尽量把矮的楼房放在高的楼房的前面,即连续的等高的一些"X"我们把它视为一座楼房. 具体的做法可以维护一个Y坐标单增的栈,从左到右读入每个坐标,将Y坐标与栈顶坐标比较: 若Y==栈顶坐标,那么接着读下面一个坐标 若Y>栈顶坐标,那么把Y坐标加入栈成为新的栈顶 若Y<栈顶坐标

BZOJ 1628 [Usaco2007 Demo]City skyline:单调栈

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1628 题意: 题解: 单调栈. 单调性: 栈内元素高度递增. 一旦出现比栈顶小的元素,则表明一栋房子的结束. 入栈: 如果出现了一个新的高度b(栈中没有),则入栈. 表明从现在开始,一定有一栋高度为b的房子,只是我们不知道它在哪里结束而已. 出栈: 对于现在的高度b,将栈中所有高度 > b的元素都出栈. 因为此时比b高的房子不得不结束. 每出栈一个元素,ans++. 注:最后要再多算一次

Code Forces Gym 100971D Laying Cables(单调栈)

D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. Whe

小结:单调栈 &amp; 单调队列

概要: 对于维护信息具有单调性的性质或者问题可以转化为具有单调性质的模型的题,我们可以考虑用单调栈或单调队列. 技巧及注意: 技巧很多,只要能将问题转化为单调性问题,就好解决了. 当维护固定长度的单调区间,我们考虑用单调队列,如[BZOJ]3314: [Usaco2013 Nov]Crowded Cows(单调队列) 单调栈维护长度时要进行及时更新,例如:[BZOJ]3039: 玉蟾宫(DP/单调栈) 假设完美状态后再进行减法原理,例如:[BZOJ]1628 && 1683: [Usaco

BZOJ_1628_[Usaco2007_Demo]_City_skyline_(单调栈)

描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1628 给出\(n\)个距形的影子,问最少是多少个建筑的?(建筑的影子可以重叠). 分析 用单调栈维护一下. 栈内是可能"延续"到当前位置的之前的影子.那么显然比当前位置高的不可能.如果有和当前位置等高的影子,就延续过来,就可以少一个建筑,否则,就向栈里加入当前位置高度的影子. 1 #include <bits/stdc++.h> 2 using namespace st

Gym 100971D Laying Cables 单调栈

Description One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. When one-dimensional country got the Internet, it was decided to place the main server

[Codeforces Round #622 (Div. 2)] - C2. Skyscrapers (hard version) (单调栈)

[Codeforces Round #622 (Div. 2)] - C2. Skyscrapers (hard version) (单调栈) C2. Skyscrapers (hard version) time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output This is a harder version of the probl

(单调栈)poj-2559 Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the