[ZJOI2007] 棋盤製作

題目描述
國際象棋是世界上最古老的博弈遊戲之一,和中國的圍棋、象棋以及日本的將棋同享盛名。據說國際象棋起源於易經的思想,棋盤是一個8*8大小的黑白相間的方陣,對應八八六十四卦,黑白對應陰陽。
而我們的主人公小Q,正是國際象棋的狂熱愛好者。作爲一個頂尖高手,他已不滿足於普通的棋盤與規則,於是他跟他的好朋友小W決定將棋盤擴大以適應他們的新規則。
小Q找到了一張由N*M個正方形的格子組成的矩形紙片,每個格子被塗有黑白兩種顏色之一。小Q想在這種紙中裁減一部分作爲新棋盤,當然,他希望這個棋盤儘可能的大。
不過小Q還沒有決定是找一個正方形的棋盤還是一個矩形的棋盤(當然,不管哪種,棋盤必須都黑白相間,即相鄰的格子不同色),所以他希望可以找到最大的正方形棋盤面積和最大的矩形棋盤面積,從而決定哪個更好一些。
於是小Q找到了即將參加全國信息學競賽的你,你能幫助他麼?


【題目分析】
行列奇偶性不同,取反,然後分別單調棧找最大子矩陣。


【代碼】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct node{int hi,wi;}sta[2001];
int n,m,map[2005][2005],h[2005][2005];
int top=0,max1=0,max2=0;
void solve()
{
    for (int i=1;i<=n;++i)
        for (int j=1;j<=m;++j)
            h[i][j]=map[i][j]?h[i-1][j]+1:0;
//    for (int i=1;i<=n;++i)
//    {
//        for (int j=1;j<=m;++j)
//            printf("%d ",h[i][j]);
//        printf("\n");
//    }
//    printf("\n");
    for (int i=1;i<=n;++i)
    {
        top=0;
        for (int j=1;j<=m+1;++j)
        {
            while (top&&sta[top].hi>h[i][j])
            {
                if ((!(top-1))||(sta[top-1].hi<h[i][j]))
                {
                    max2=max(max2,sta[top].wi*sta[top].hi);
                    max1=max(max1,min(sta[top].wi,sta[top].hi)*min(sta[top].wi,sta[top].hi));
                    sta[top].hi=h[i][j];
                }
                else{
                    max2=max(max2,sta[top].wi*sta[top].hi);
                    max1=max(max1,min(sta[top].wi,sta[top].hi)*min(sta[top].wi,sta[top].hi));
                    top--;
                    sta[top].wi+=sta[top+1].wi;
                }
            }
            if (top&&sta[top].hi==h[i][j]) sta[top].wi++;
            else
            {
                top++;
                sta[top].hi=h[i][j];
                sta[top].wi=1;
            }
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;++i)
        for (int j=1;j<=m;++j)
        {
            scanf("%d",&map[i][j]);
            if ((i^j)&1) map[i][j]^=1;
        }
//    for (int i=1;i<=n;++i)
//    {
//        for (int j=1;j<=m;++j)
//            printf("%d ",map[i][j]);
//        printf("\n");
//    }
//    printf("\n");
    solve();
    for (int i=1;i<=n;++i)
        for (int j=1;j<=m;++j)
            map[i][j]^=1;
//    for (int i=1;i<=n;++i)
//    {
//        for (int j=1;j<=m;++j)
//            printf("%d ",map[i][j]);
//        printf("\n");
//    }
//    printf("\n");
    solve();
    printf("%d\n%d\n",max1,max2);
}
發佈了505 篇原創文章 · 獲贊 4 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章