典型90問    次の典型90問へ    前の典型90問へ

典型90問 036 Max Manhattan Distance(★5)


問題へのリンク


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

    struct XYInfoDef
    {
        internal long X;
        internal long Y;
    }
    static List<XYInfoDef> mXYInfoList = new List<XYInfoDef>();

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

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

        SplitAct(InputList[0]);
        long N = wkArr[0];

        foreach (string EachStr in InputList.Skip(1).Take((int)N)) {
            SplitAct(EachStr);
            long X = wkArr[0];
            long Y = wkArr[1];

            // 45度回転した座標で考える
            XYInfoDef WillAdd;
            WillAdd.X = X - Y;
            WillAdd.Y = X + Y;
            mXYInfoList.Add(WillAdd);
        }

        long MinX = mXYInfoList.Min(pX => pX.X);
        long MaxX = mXYInfoList.Max(pX => pX.X);
        long MinY = mXYInfoList.Min(pX => pX.Y);
        long MaxY = mXYInfoList.Max(pX => pX.Y);

        foreach (string EachStr in InputList.Skip(1 + (int)N)) {
            int Q = int.Parse(EachStr);

            long CurrX = mXYInfoList[Q - 1].X;
            long CurrY = mXYInfoList[Q - 1].Y;
            var AnswerKouho = new List<long>();
            AnswerKouho.Add(Math.Abs(CurrX - MinX));
            AnswerKouho.Add(Math.Abs(CurrX - MaxX));
            AnswerKouho.Add(Math.Abs(CurrY - MinY));
            AnswerKouho.Add(Math.Abs(CurrY - MaxY));
            Console.WriteLine(AnswerKouho.Max());
        }
    }
}


解説

マンハッタン距離なので、45度回転した座標で考えてます。