---
id: 309
lang: en
translation_of: index.md
source_sha256: decb52bcc898ee099ba81c4b87e8ebd87692f78da7d26ba36b8acd66889b53e8
title: "CH1803: City Game — Solution"
description: "Using a monotonic stack row by row to find how far each column can extend up, left and right, and taking the maximum."
tags: ["OI","Monotonic Stack"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## Approach

While thinking about it, I could vaguely sense that this should be done with a monotonic stack, but I never managed to figure out the details of how. Now that I understand it, let me write down some thoughts.

<!--more-->

The strategy for this problem is to use a monotonic stack to rule out irrelevant answers. While reading each row, we can compute how far **the column parts** (because we compute while reading each row, which means that when we compute, the reading is not finished yet) can extend upward, leftward and rightward, and take the maximum.

I wrote the comments in the code. Just read the code.

## Code

```cpp
// CH1803.cpp
// includings;
#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
const int maxn = 1010;
// n,m stands for the boundaries;
// h stands for the height of streching up;
// l stands for the left bound;
// r stands for the right bound;
int n, m, h[maxn], l[maxn], r[maxn], ans;
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            char ch;
            while (ch = getchar())
                if (ch == 'F' || ch == 'R')
                    break;
            // do statistics on the max height;
            if (ch == 'F')
                h[j]++;
            else
                h[j] = 0;
        }
        // rese;
        memset(l, 0, sizeof(l));
        memset(r, 0, sizeof(r));
        stack<int> st;
        for (int i = 1; i <= m; i++)
        {
            // delete the higher ones;
            while (!st.empty() && h[i] <= h[st.top()])
                st.pop();
            // set the bound;
            if (!st.empty())
                l[i] = st.top();
            else
                l[i] = 0;
            st.push(i);
        }
        // clear the stack;
        while (!st.empty())
            st.pop();
        for (int i = m; i >= 1; i--)
        {
            // delete the higher ones;
            while (!st.empty() && h[i] <= h[st.top()])
                st.pop();
            // set the right bound;
            if (!st.empty())
                r[i] = st.top();
            else
                r[i] = m + 1;
            st.push(i);
        }
        // do statistics on this;
        for (int i = 1; i <= m; i++)
            ans = max(ans, (h[i] * ((r[i] - 1) - (l[i] + 1) + 1)));
    }
    // output it;
    printf("%d\n", 3 * ans);
    return 0;
}
```
