AOJ本の読書メモ   AOJ    次のAOJの問題へ    前のAOJの問題へ

DPL_3_B: Largest Rectangle


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

// Q077 最大長方形 https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_3_B&lang=jp
class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("4 5");
            WillReturn.Add("0 0 1 0 0");
            WillReturn.Add("1 0 0 0 0");
            WillReturn.Add("0 0 0 1 0");
            WillReturn.Add("0 0 0 1 0");
            //6
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    const int MAX = 1400;

    struct Rectangle
    {
        internal int Height;
        internal int Pos;
    }

    static int GetLargestRectangle(List<int> pHistList)
    {
        var Stk = new Stack<Rectangle>();
        int MaxArea = 0;
        pHistList.Add(0);

        for (int I = 0; I <= pHistList.Count - 1; I++) {
            Rectangle Rect;
            Rect.Height = pHistList[I];
            Rect.Pos = I;
            if (Stk.Count == 0) {
                Stk.Push(Rect);
            }
            else if (Stk.Peek().Height < Rect.Height) {
                Stk.Push(Rect);
            }
            else if (Stk.Peek().Height > Rect.Height) {
                int Target = I;
                while (Stk.Count > 0 && Stk.Peek().Height >= Rect.Height) {
                    Rectangle Pre = Stk.Pop();
                    int Area = Pre.Height * (I - Pre.Pos);
                    MaxArea = Math.Max(MaxArea, Area);
                    Target = Pre.Pos;
                }
                Rect.Pos = Target;
                Stk.Push(Rect);
            }
        }
        return MaxArea;
    }

    static int H, W;
    static int[,] mBanArr = new int[MAX, MAX];
    static int[,] mSeqCntArr = new int[MAX, MAX];

    static int GetLargestRectangle()
    {
        for (int J = 0; J < W; J++) {
            for (int I = 0; I < H; I++) {
                // 1が汚れたタイルで、0が綺麗なタイル
                if (mBanArr[I, J] == 1) {
                    mSeqCntArr[I, J] = 0;
                }
                else {
                    mSeqCntArr[I, J] = (I > 0) ? mSeqCntArr[I - 1, J] + 1 : 1;
                }
            }
        }

        int MaxArea = 0;
        for (int I = 0; I < H; I++) {
            var HistList = new List<int>();
            for (int J = 0; J <= mSeqCntArr.GetUpperBound(1); J++) {
                HistList.Add(mSeqCntArr[I, J]);
            }
            int Result = GetLargestRectangle(HistList);
            MaxArea = Math.Max(MaxArea, Result);
        }
        return MaxArea;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        SplitAct(InputList[0]);
        H = wkArr[0];
        W = wkArr[1];

        for (int I = 0; I < H; I++) {
            SplitAct(InputList[I + 1]);
            for (int J = 0; J < W; J++) {
                mBanArr[I, J] = wkArr[J];
            }
        }
        Console.WriteLine(GetLargestRectangle());
    }
}


解説

ヒストグラムの高さを順次見ながら、
下記の3通りで処理を分岐させてます。

場合1
スタックが空の場合

場合2
スタックのトップにある長方形の高さが、カレントの長方形より低い場合

場合3
スタックのトップにある長方形の高さが、カレントの長方形より高い場合