AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC203-D Pond


問題へのリンク


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 2");
            WillReturn.Add("1 7 0");
            WillReturn.Add("5 8 11");
            WillReturn.Add("10 4 2");
            //4
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 3");
            WillReturn.Add("1 2 3");
            WillReturn.Add("4 5 6");
            WillReturn.Add("7 8 9");
            //5
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long mK;

    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();
        mK = wkArr[1];

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

        // 二分法で、最小の中央値を返す
        long L = -1;
        long R = mBanArr.Cast<long>().Max();
        while (L + 1 < R) {
            long Mid = (L + R) / 2;

            if (HasMedian(Mid)) {
                R = Mid;
            }
            else {
                L = Mid;
            }
        }
        Console.WriteLine(R);
    }

    // 引数以下な中央値が存在するかを返す
    static bool HasMedian(long pTargetMedian)
    {
        long Banme = mK * mK / 2 + 1;
        long Cnt = mK * mK;
        long NeedCnt = Cnt - Banme + 1;

        long[,] RunSumArr = CreateRunSumArr(pTargetMedian);
        for (long LoopX = 0; LoopX <= UB_X; LoopX++) {
            long EndX = LoopX + mK - 1;
            if (EndX > UB_X) break;
            for (long LoopY = 0; LoopY <= UB_Y; LoopY++) {
                long EndY = LoopY + mK - 1;
                if (EndY > UB_Y) break;

                long CurrSumCnt = DeriveSumRect(RunSumArr, EndX, EndY);
                CurrSumCnt -= DeriveSumRect(RunSumArr, LoopX - 1, EndY);
                CurrSumCnt -= DeriveSumRect(RunSumArr, EndX, LoopY - 1);
                CurrSumCnt += DeriveSumRect(RunSumArr, LoopX - 1, LoopY - 1);

                if (CurrSumCnt >= NeedCnt) {
                    return true;
                }
            }
        }
        return false;
    }

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

    // 閾値以下なら1、閾値未満なら0を設定した二次元配列を作成し、
    // 右方向と、下方向の、累積和を設定して、返す
    static long[,] CreateRunSumArr(long pShikii)
    {
        long[,] RunSumArr = new long[UB_X + 1, UB_Y + 1];
        for (long LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (long LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (mBanArr[LoopX, LoopY] <= pShikii) {
                    RunSumArr[LoopX, LoopY] = 1;
                }
            }
        }

        // 累積和を設定する (横方向)
        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];
            }
        }

        return RunSumArr;
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をintの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 (long Y = 0; Y <= UB_Y; Y++) {
            SplitAct(StrList[(int)Y]);
            for (long X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = IntArr[X];
            }
        }
        return WillReturn;
    }
}


解説

中央値がX以下であること ⇔ X以下の数が必要数以上ある
という同値な変換を行って、

二分法と二次元累積和を組み合わせてます。