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

ABC461-D Count Subgrid Sum = K


問題へのリンク


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 3");
            WillReturn.Add("1001");
            WillReturn.Add("1101");
            WillReturn.Add("0110");
            //8
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 4 20");
            WillReturn.Add("0101");
            WillReturn.Add("1010");
            WillReturn.Add("0101");
            WillReturn.Add("1010");
            WillReturn.Add("0101");
            //0
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("15 20 17");
            WillReturn.Add("10111101101100000100");
            WillReturn.Add("01100000000010000011");
            WillReturn.Add("01110010111000111000");
            WillReturn.Add("11001100000111011000");
            WillReturn.Add("10100001100011100010");
            WillReturn.Add("01101000101010000101");
            WillReturn.Add("10110001111110000100");
            WillReturn.Add("10110011101100101101");
            WillReturn.Add("01010001110011001001");
            WillReturn.Add("01010110010001100110");
            WillReturn.Add("01110100011110011110");
            WillReturn.Add("01100000100111010010");
            WillReturn.Add("11001101100111101100");
            WillReturn.Add("10111100010101111011");
            WillReturn.Add("00101101011100010000");
            //448
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long[] GetSplitArr(string pStr)
    {
        return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
    }

    static long mK;
    static long[,] mRunSumArr;
    static long UB_X;
    static long UB_Y;

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] wkArr = GetSplitArr(InputList[0]);
        mK = wkArr[2];

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

        mRunSumArr = 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 (BanArr[LoopX, LoopY] == '1') {
                    mRunSumArr[LoopX, LoopY]++;
                }
            }
        }

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

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

        long Answer = 0;
        for (long StaY = 0; StaY <= UB_Y; StaY++) {
            for (long EndY = StaY; EndY <= UB_Y; EndY++) {
                Answer += ExecSyakutori(StaY, EndY);
            }
        }
        Console.WriteLine(Answer);
    }

    // 尺取法
    static long ExecSyakutori(long pStaY, long pEndY)
    {
        long Answer = 0;

        long R1 = 0;
        long R2 = 0;
        for (long L = 0; L <= UB_X; L++) {
            R1 = Math.Max(L, R1);
            R2 = Math.Max(L, R2);

            while (R1 + 1 <= UB_X && DeriveSumRect(L, pStaY, R1 + 1, pEndY) <= mK) {
                R1++;
            }
            while (R2 + 1 <= UB_X && DeriveSumRect(L, pStaY, R2 + 1, pEndY) <= mK - 1) {
                R2++;
            }
            if (DeriveSumRect(L, pStaY, R1, pEndY) <= mK) {
                Answer += R1 - L + 1;
            }
            if (DeriveSumRect(L, pStaY, R2, pEndY) <= mK - 1) {
                Answer -= R2 - L + 1;
            }
        }

        return Answer;
    }

    // (pStaX,pStaY)と(pEndX,pEndY)からなる矩形の和を求める 
    static long DeriveSumRect(long pStaX, long pStaY, long pEndX, long pEndY)
    {
        long CurrSum = DeriveSumRectZero(pEndX, pEndY);
        CurrSum -= DeriveSumRectZero(pStaX - 1, pEndY);
        CurrSum -= DeriveSumRectZero(pEndX, pStaY - 1);
        CurrSum += DeriveSumRectZero(pStaX - 1, pStaY - 1);
        return CurrSum;
    }

    // (0,0)と(pEndX,pEndY)からなる矩形の和を求める 
    static long DeriveSumRectZero(long pX, long pY)
    {
        if (pX < 0) return 0;
        if (pY < 0) return 0;
        return mRunSumArr[pX, pY];
    }

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

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

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


解説

Y座標の開始と終了の組合せを全探索してから、
1の個数がK以下の区間数と
1の個数がK-1以下の区間数を
尺取法で求めてます。

1の個数がKの区間数は、
(1の個数がK以下の区間数) - (1の個数がK-1以下の区間数)
で求めることができます。