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

AOJ 0658 勇者ビ太郎


問題へのリンク


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("JOIJ");
            WillReturn.Add("JIOO");
            WillReturn.Add("IIII");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4 4");
            WillReturn.Add("JJOO");
            WillReturn.Add("JJOO");
            WillReturn.Add("IIJO");
            WillReturn.Add("IIIJ");
            //17
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

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

        // 行ごとのオーブのX座標
        var OPosListDict = new Dictionary<long, List<long>>();
        for (long LoopY = 0; LoopY <= UB_Y; LoopY++) {
            OPosListDict[LoopY] = new List<long>();
        }

        // 列ごとの金塊のY座標
        var IPosListDict = new Dictionary<long, List<long>>();
        for (long LoopX = 0; LoopX <= UB_X; LoopX++) {
            IPosListDict[LoopX] = new List<long>();
        }

        for (long LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (long LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (BanArr[LoopX, LoopY] == 'O') OPosListDict[LoopY].Add(LoopX);
                if (BanArr[LoopX, LoopY] == 'I') IPosListDict[LoopX].Add(LoopY);
            }
        }

        long Answer = 0;
        for (long LoopX = 0; LoopX <= UB_X; LoopX++) {
            List<long> IPosList = IPosListDict[LoopX];

            for (long LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (BanArr[LoopX, LoopY] != 'J') continue;

                List<long> OPosList = OPosListDict[LoopY];

                long ResultInd1 = ExecNibunhou_UpperBound(LoopX, OPosList);
                if (ResultInd1 == -1) continue;
                long ResultInd2 = ExecNibunhou_UpperBound(LoopY, IPosList);
                if (ResultInd2 == -1) continue;

                long Cnt1 = OPosList.Count - 1 - ResultInd1 + 1;
                long Cnt2 = IPosList.Count - 1 - ResultInd2 + 1;

                Answer += Cnt1 * Cnt2;
            }
        }
        Console.WriteLine(Answer);
    }

    // 二分法で、Val超えで最小の値を持つ、添字を返す
    static long ExecNibunhou_UpperBound(long pVal, List<long> pList)
    {
        // 要素が0件のケース
        if (pList.Count == 0) return -1;

        // 最後の要素がVal以下の特殊ケース
        if (pVal >= pList.Last()) {
            return -1;
        }
        // 最初の要素がVal超えの特殊ケース
        if (pVal < pList[0]) {
            return 0;
        }

        long L = 0;
        long R = pList.Count - 1;

        while (L + 1 < R) {
            long Mid = (L + R) / 2;

            if (pList[(int)Mid] > pVal) {
                R = Mid;
            }
            else {
                L = Mid;
            }
        }
        return R;
    }

    ////////////////////////////////////////////////////////////////
    // 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;
    }
}


解説

行ごとのオーブのX座標と
列ごとの金塊のY座標を
最初に用意しておき、
Jごとに、二分探索でオーブの個数と金塊の個数を求め、
積の法則を使ってます。