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

DSL_5_B: The Maximum Number of Overlaps


問題へのリンク


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

    struct RectInfoDef
    {
        internal long XMin;
        internal long YMin;
        internal long XMax;
        internal long YMax;
    }
    static List<RectInfoDef> mRectInfoList = new List<RectInfoDef>();

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

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

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            RectInfoDef WillAdd;
            long X1 = wkArr[0];
            long Y1 = wkArr[1];
            long X2 = wkArr[2];
            long Y2 = wkArr[3];
            WillAdd.XMin = Math.Min(X1, X2);
            WillAdd.XMax = Math.Max(X1, X2);
            WillAdd.YMin = Math.Min(Y1, Y2);
            WillAdd.YMax = Math.Max(Y1, Y2);

            WillAdd.XMax--;
            WillAdd.YMax--;

            mRectInfoList.Add(WillAdd);
        }

        long UB_X = mRectInfoList.Max(pX => pX.XMax);
        long UB_Y = mRectInfoList.Max(pX => pX.YMax);

        long[,] ImosArr = new long[UB_X + 1, UB_Y + 1];
        foreach (RectInfoDef EachRectInfo in mRectInfoList) {
            long StaX = EachRectInfo.XMin;
            long StaY = EachRectInfo.YMin;
            long EndX = EachRectInfo.XMax + 1;
            long EndY = EachRectInfo.YMax + 1;

            ImosArr[StaX, StaY]++;
            if (EndX <= UB_X) {
                ImosArr[EndX, StaY]--;
            }
            if (EndY <= UB_Y) {
                ImosArr[StaX, EndY]--;
            }
            if (EndX <= UB_X && EndY <= UB_Y) {
                ImosArr[EndX, EndY]++;
            }
        }


        // 横方向の累積和を求める
        for (int X = 1; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                ImosArr[X, Y] += ImosArr[X - 1, Y];
            }
        }

        // 縦方向の累積和を求める
        for (int Y = 1; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                ImosArr[X, Y] += ImosArr[X, Y - 1];
            }
        }

        var AnswerSet = new HashSet<long>();
        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                AnswerSet.Add(ImosArr[X, Y]);
            }
        }
        Console.WriteLine(AnswerSet.Max());
    }
}


解説

2次元いもす法で解いてます。