【單調(diào)棧】POJ_3494_Largest Submatrix of All 1’s

Largest Submatrix of All 1’s

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 5903 Accepted: 2226
Case Time Limit: 2000MS

Description
Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements.

Input
The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on m lines each with n numbers. The input ends once EOF is met.

Output
For each test case, output one line containing the number of elements of the largest submatrix of all 1’s. If the given matrix is of all 0’s, output 0.

Sample Input
2 2
0 0
0 0
4 4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0

Sample Output
0
4

Source
POJ Founder Monthly Contest – 2008.01.31, xfxyjwf

題意:
給一個(gè)m*n的01矩陣,找出其中最大的全1矩陣,輸出其1的個(gè)數(shù)。

思路:
單調(diào)棧。同POJ2559,對(duì)每一行的每一個(gè)元素做預(yù)處理,將其轉(zhuǎn)換成m個(gè)POJ2559問(wèn)題,遍歷所有行即可找出最大,時(shí)間復(fù)雜度約O(mn)。

#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 2000;

struct Node {
    int height;
    int index;
}st[maxn + 5];
int top;

int buf[maxn + 5];
int m, n, ans;

int main() {
    int value, tmp;
    while (scanf("%d%d", &m, &n) != EOF) {
        memset(buf, 0, sizeof(buf));
        ans = 0;
        for (int i = 1; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                scanf("%d", &value);
                if (value == 0)
                    buf[j] = 0;
                else
                    buf[j] += 1;
            }
            buf[n + 1] = -1; // 最后包含一個(gè)-1的項(xiàng),用于彈出棧中所有元素
            st[0].height = -1; // 起始項(xiàng)
            st[0].index = 0;
            top = 1;
            for (int k = 1; k <= n + 1; ++k) {
                // 進(jìn)棧
                if (buf[k] >= st[top - 1].height) {
                    st[top].height = buf[k];
                    st[top].index = k;
                    ++top;
                }
                // 循環(huán)出棧,維護(hù)棧單調(diào)
                else {
                    while (buf[k] < st[top - 1].height) {
                        tmp = st[top - 1].height * (k - st[top - 1].index);
                        if (ans < tmp)
                            ans = tmp;
                        --top;
                    }
                    st[top++].height = buf[k];
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 首先我們來(lái)看看MVC架構(gòu)的示意圖: 和訪問(wèn)者交互的是控制層(Controller層),控制器(controller...
    司馬東陽(yáng)閱讀 352評(píng)論 0 0
  • 你覺(jué)得這個(gè)世界上最難追上的人是誰(shuí)? 初中的時(shí)候,我覺(jué)得,世界上最難追上的人是劉翔博爾特,高中時(shí)身邊的小伙伴們開始情...
    晏耀飛閱讀 377評(píng)論 0 3
  • 本周某日上午某人因某稿被某領(lǐng)導(dǎo)大罵一番,當(dāng)著某某某同事的面兒,還是,當(dāng)然,以上是客觀情況,主觀上某人著實(shí)能為自己辯...
    競(jìng)走的蝸牛閱讀 361評(píng)論 0 2
  • 此刻只能用一個(gè)貓頭來(lái)形容心情
    成金閱讀 212評(píng)論 0 2
  • 高樓大廈排排座, 四處無(wú)風(fēng)夜難眠; 風(fēng)扇一晚不停息, 大汗如雨淚漣漣。 注1:長(zhǎng)沙不愧有火爐之稱,連續(xù)高溫,又高樓...
    亮靚_27d5閱讀 1,133評(píng)論 32 48