AtCoderの有志コンテスト    次の有志コンテストの問題へ    前の有志コンテストの問題へ

GigaCode 2019 D 家の建設


問題へのリンク


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("1 1 200 500");
            WillReturn.Add("300");
            //1
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("1 8 10 200");
            WillReturn.Add("30 40 10 20 30 40 10 20");
            //6
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("5 5 10 17");
            WillReturn.Add("12 19 25 13 25");
            WillReturn.Add("14 16 18 11 10");
            WillReturn.Add("19 17 24 26 12");
            WillReturn.Add("23 11 16 19 14");
            WillReturn.Add("18 23 27 11 16");
            //0
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("4 7 10 240");
            WillReturn.Add("17 12 15 18 19 15 23");
            WillReturn.Add("22 12 41 16 27 10 10");
            WillReturn.Add("15 69 18 11 10 23 15");
            WillReturn.Add("12 20 13 12 17 18 15");
            //9
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long[,] mBanArr;
    static long UB_X;
    static long UB_Y;

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long K = wkArr[2];
        long V = wkArr[3];

        mBanArr = CreateBanArr(InputList.Skip(1));
        UB_X = mBanArr.GetUpperBound(0);
        UB_Y = mBanArr.GetUpperBound(1);


        long[,] RunSumArr = (long[,])mBanArr.Clone();

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

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

        long Answer = 0;
        for (long StaX = 0; StaX <= UB_X; StaX++) {
            for (long EndX = StaX; EndX <= UB_X; EndX++) {
                for (long StaY = 0; StaY <= UB_Y; StaY++) {
                    for (long EndY = StaY; EndY <= UB_Y; EndY++) {
                        long CostSum = DeriveSumRect(RunSumArr, EndX, EndY);
                        CostSum -= DeriveSumRect(RunSumArr, StaX - 1, EndY);
                        CostSum -= DeriveSumRect(RunSumArr, EndX, StaY - 1);
                        CostSum += DeriveSumRect(RunSumArr, StaX - 1, StaY - 1);

                        long Area = (EndX - StaX + 1) * (EndY - StaY + 1);
                        CostSum += Area * K;

                        if (CostSum <= V) {
                            Answer = Math.Max(Answer, Area);
                        }
                        else {
                            break;
                        }
                    }
                }
            }
        }
        Console.WriteLine(Answer);
    }

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

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

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

        SplitAct(StrList[0]);

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

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

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


解説

2次元累積和を使ってます。