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

ABC374-D Laser Marking


問題へのリンク


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 1");
            WillReturn.Add("1 3 2 1");
            WillReturn.Add("0 2 0 0");
            WillReturn.Add("3 0 2 0");
            //6.44317475868633722080
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 1 1");
            WillReturn.Add("0 0 10 10");
            WillReturn.Add("0 2 2 0");
            //20.97056274847714058517
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("6 3 2");
            WillReturn.Add("-1000 -1000 1000 1000");
            WillReturn.Add("1000 -1000 -1000 1000");
            WillReturn.Add("-1000 -1000 1000 1000");
            WillReturn.Add("1000 -1000 -1000 1000");
            WillReturn.Add("1000 1000 -1000 -1000");
            WillReturn.Add("-1000 1000 1000 -1000");
            //9623.35256169626864153344
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("6 10 8");
            WillReturn.Add("1000 1000 -1000 -1000");
            WillReturn.Add("1000 -1000 -1000 -1000");
            WillReturn.Add("-1000 1000 1000 1000");
            WillReturn.Add("-1000 1000 -1000 -1000");
            WillReturn.Add("1000 1000 1000 -1000");
            WillReturn.Add("1000 -1000 -1000 1000");
            //2048.52813742385702910909
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct SenbunDef
    {
        internal double StaX;
        internal double StaY;
        internal double EndX;
        internal double EndY;
    }
    static List<SenbunDef> mSenbunList = new List<SenbunDef>();

    struct PointDef
    {
        internal double X;
        internal double Y;
    }

    static double mS;
    static double mT;

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

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

        SplitAct(InputList[0]);
        mS = wkArr[1];
        mT = wkArr[2];

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            SenbunDef WillAdd;
            WillAdd.StaX = wkArr[0];
            WillAdd.StaY = wkArr[1];
            WillAdd.EndX = wkArr[2];
            WillAdd.EndY = wkArr[3];
            mSenbunList.Add(WillAdd);
        }

        var SenbunIndList = new List<int>();
        for (int I = 0; I <= mSenbunList.Count - 1; I++) {
            SenbunIndList.Add(I);
        }

        var tmp1 = RubyPatternClass<int>.Permutation(SenbunIndList, SenbunIndList.Count).ToList();

        var BaseList = new List<int>();
        BaseList.Add(0);
        BaseList.Add(1);
        var tmp2 = RubyPatternClass<int>.RepeatedPermutation(BaseList, SenbunIndList.Count).ToList();

        double Answer = double.MaxValue;
        foreach (int[] EachArr1 in tmp1) {
            foreach (int[] EachArr2 in tmp2) {
                double CurrAnswer = DeriveAnswer(EachArr1, EachArr2);
                Answer = Math.Min(Answer, CurrAnswer);
            }
        }
        Console.WriteLine(Answer);
    }

    // 配列2つを引数として解候補を返す
    static double DeriveAnswer(int[] pArr1, int[] pArr2)
    {
        int UB = pArr1.GetUpperBound(0);
        var PointList = new List<PointDef>();
        PointDef WillAdd;

        WillAdd.X = 0;
        WillAdd.Y = 0;
        PointList.Add(WillAdd);

        for (int I = 0; I <= UB; I++) {
            PointDef WillAdd1;
            WillAdd1.X = mSenbunList[pArr1[I]].StaX;
            WillAdd1.Y = mSenbunList[pArr1[I]].StaY;
            PointDef WillAdd2;
            WillAdd2.X = mSenbunList[pArr1[I]].EndX;
            WillAdd2.Y = mSenbunList[pArr1[I]].EndY;
            if (pArr2[I] == 0) {
                PointList.Add(WillAdd1);
                PointList.Add(WillAdd2);
            }
            else {
                PointList.Add(WillAdd2);
                PointList.Add(WillAdd1);
            }
        }

        double Answer = 0;
        for (int I = 1; I <= PointList.Count - 1; I++) {
            double StaX = PointList[I - 1].X;
            double StaY = PointList[I - 1].Y;
            double EndX = PointList[I].X;
            double EndY = PointList[I].Y;

            double Distance = DeriveDistance(StaX, StaY, EndX, EndY);
            if (I % 2 == 1) {
                Answer += Distance / mS;
            }
            else {
                Answer += Distance / mT;
            }
        }
        return Answer;
    }

    // ベクトル2つの距離を返す
    static double DeriveDistance(double pStaX, double pStaY, double pEndX, double pEndY)
    {
        double wkDiff1 = pStaX - pEndX;
        double wkDiff2 = pStaY - pEndY;
        return Math.Sqrt(wkDiff1 * wkDiff1 + wkDiff2 * wkDiff2);
    }
}

#region RubyPatternClass
// Rubyの場合の数
internal static class RubyPatternClass<Type>
{
    // 順列を返す
    private struct JyoutaiDef_Permutation
    {
        internal List<int> SelectedIndList;
    }
    internal static IEnumerable<Type[]> Permutation(IEnumerable<Type> pEnum, int pR)
    {
        if (pR == 0) yield break;
        Type[] pArr = pEnum.ToArray();
        if (pArr.Length < pR) yield break;

        var Stk = new Stack<JyoutaiDef_Permutation>();
        JyoutaiDef_Permutation WillPush;
        for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
            WillPush.SelectedIndList = new List<int>() { I };
            Stk.Push(WillPush);
        }

        while (Stk.Count > 0) {
            JyoutaiDef_Permutation Popped = Stk.Pop();

            // クリア判定
            if (Popped.SelectedIndList.Count == pR) {
                var WillReturn = new List<Type>();
                Popped.SelectedIndList.ForEach(X => WillReturn.Add(pArr[X]));
                yield return WillReturn.ToArray();
                continue;
            }

            for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
                if (Popped.SelectedIndList.Contains(I)) continue;
                WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList) { I };
                Stk.Push(WillPush);
            }
        }
    }

    // 重複順列を返す
    private struct JyoutaiDef_RepeatedPermutation
    {
        internal List<int> SelectedIndList;
    }
    internal static IEnumerable<Type[]> RepeatedPermutation(IEnumerable<Type> pEnum, int pR)
    {
        if (pR == 0) yield break;
        Type[] pArr = pEnum.ToArray();

        var Stk = new Stack<JyoutaiDef_RepeatedPermutation>();
        JyoutaiDef_RepeatedPermutation WillPush;
        for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
            WillPush.SelectedIndList = new List<int>() { I };
            Stk.Push(WillPush);
        }

        while (Stk.Count > 0) {
            JyoutaiDef_RepeatedPermutation Popped = Stk.Pop();

            // クリア判定
            if (Popped.SelectedIndList.Count == pR) {
                var WillReturn = new List<Type>();
                Popped.SelectedIndList.ForEach(X => WillReturn.Add(pArr[X]));
                yield return WillReturn.ToArray();
                continue;
            }

            for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
                WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList) { I };
                Stk.Push(WillPush);
            }
        }
    }
}
#endregion


解説

線分の描画順列の全探索と
線分のどっちから描画するかを全探索してます。