AtCoderのARC    次のARCの問題へ    前のARCの問題へ

ARC025-B チョコレート


問題へのリンク


C#のソース

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

class Program
{
    static string InputPattern = "InputX";

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

        if (InputPattern == "Input1") {
            WillReturn.Add("3 4");
            WillReturn.Add("4 6 2 5");
            WillReturn.Add("3 5 6 7");
            WillReturn.Add("2 5 5 6");
            //6
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 2");
            WillReturn.Add("4 0");
            WillReturn.Add("7 3");
            //4
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("2 3");
            WillReturn.Add("0 0 0");
            WillReturn.Add("1 2 3");
            //3
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("3 3");
            WillReturn.Add("1 2 3");
            WillReturn.Add("6 5 4");
            WillReturn.Add("7 8 9");
            //0
        }
        else if (InputPattern == "Input5") {
            WillReturn.Add("1 5");
            WillReturn.Add("0 1 2 3 4");
            //1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        int[,] BanArr = CreateBanArr(InputList.Skip(1));
        int UB_X = BanArr.GetUpperBound(0);
        int UB_Y = BanArr.GetUpperBound(1);

        // 黒のチョコは、-1を掛ける
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if ((LoopX + LoopY) % 2 == 0) {
                    BanArr[LoopX, LoopY] *= -1;
                }
            }
        }

        int[,] RunSumArr = (int[,])BanArr.Clone();

        // 累積和を設定する (横方向)
        for (int LoopX = 1; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                RunSumArr[LoopX, LoopY] += RunSumArr[LoopX - 1, LoopY];
            }
        }

        // 累積和を設定する (縦方向)
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 1; LoopY <= UB_Y; LoopY++) {
                RunSumArr[LoopX, LoopY] += RunSumArr[LoopX, LoopY - 1];
            }
        }

        // PrintBan(RunSumArr);

        int Answer = 0;
        for (int StaX = 0; StaX <= UB_X; StaX++) {
            for (int EndX = StaX; EndX <= UB_X; EndX++) {
                for (int StaY = 0; StaY <= UB_Y; StaY++) {
                    for (int EndY = StaY; EndY <= UB_Y; EndY++) {
                        int CurrSum = DeriveSumRect(RunSumArr, EndX, EndY);
                        CurrSum -= DeriveSumRect(RunSumArr, StaX - 1, EndY);
                        CurrSum -= DeriveSumRect(RunSumArr, EndX, StaY - 1);
                        CurrSum += DeriveSumRect(RunSumArr, StaX - 1, StaY - 1);
                        if (CurrSum == 0) {
                            //Console.WriteLine("解を発見。({0},{1})から({2},{3})", StaX, StaY, EndX, EndY);
                            int AnswerKouho = (EndX - StaX + 1) * (EndY - StaY + 1);
                            Answer = Math.Max(Answer, AnswerKouho);
                        }
                    }
                }
            }
        }
        Console.WriteLine(Answer);
    }

    // (0,0)と(pX,pY)からなる長方形の数の和を返す
    static int DeriveSumRect(int[,] pRunSumArr, int pX, int pY)
    {
        if (pX < 0) return 0;
        if (pY < 0) return 0;
        return pRunSumArr[pX, pY];
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をintの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static int[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new int[0, 0];
        }

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

        SplitAct(StrList[0]);

        int UB_X = IntArr.GetUpperBound(0);
        int UB_Y = StrList.Count - 1;

        int[,] WillReturn = new int[UB_X + 1, UB_Y + 1];

        for (int Y = 0; Y <= UB_Y; Y++) {
            SplitAct(StrList[Y]);
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = IntArr[X];
            }
        }
        return WillReturn;
    }

    ////////////////////////////////////////////////////////////////
    // 2次元配列(int型)のデバッグ出力
    ////////////////////////////////////////////////////////////////
    static void PrintBan(int[,] pBanArr)
    {
        for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
            for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
                Console.Write("{0,3}", pBanArr[X, Y]);
            }
            Console.WriteLine();
        }
    }
}


解説

黒のチョコは、マイナスを濃度に掛けるようにすれば
和が0になる長方形を求める問題に帰着できるので、
二次元累積和を使ってます。