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

ABC216-D Pair of Balls


問題へのリンク


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

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

        var BallQueueList = new List<Queue<int>>();
        long BallCnt = 0;
        for (int I = 2; I <= InputList.Count - 1; I += 2) {
            int[] CurrBallArr = InputList[I].Split(' ').Select(pX => int.Parse(pX)).ToArray();
            var CurrQue = new Queue<int>(CurrBallArr);
            BallQueueList.Add(CurrQue);
            BallCnt += CurrBallArr.Length;
        }
        Queue<int>[] BallQueueArr = BallQueueList.ToArray();

        // キュー配列の添字のList[色]なDict
        var IndListDict = new Dictionary<int, List<int>>();
        var RemoveKouhoStk = new Stack<int>();
        for (int I = 0; I <= BallQueueArr.GetUpperBound(0); I++) {
            Queue<int> CurrQue = BallQueueArr[I];

            int FrontBallColor = CurrQue.Peek();
            if (IndListDict.ContainsKey(FrontBallColor) == false) {
                IndListDict[FrontBallColor] = new List<int>();
            }
            IndListDict[FrontBallColor].Add(I);
            if (IndListDict[FrontBallColor].Count == 2) {
                RemoveKouhoStk.Push(FrontBallColor);
            }
        }

        long RemoveCnt = 0;
        while (RemoveKouhoStk.Count > 0) {
            int RemoveColor = RemoveKouhoStk.Pop();

            // 2回繰り返される
            foreach (int EachInd in IndListDict[RemoveColor]) {
                Queue<int> CurrQue = BallQueueArr[EachInd];
                CurrQue.Dequeue();
                RemoveCnt++;
                if (CurrQue.Count > 0) {
                    int FrontBallColor = CurrQue.Peek();
                    if (IndListDict.ContainsKey(FrontBallColor) == false) {
                        IndListDict[FrontBallColor] = new List<int>();
                    }
                    IndListDict[FrontBallColor].Add(EachInd);
                    if (IndListDict[FrontBallColor].Count == 2) {
                        RemoveKouhoStk.Push(FrontBallColor);
                    }
                }
            }
        }

        if (RemoveCnt == BallCnt) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }
}


解説

この問題で、下記の知見を得ました。

ListクラスでAddして、RemoveAtするような用途の場合
StackクラスやQueueクラスのほうが、
ソースがシンプルになることがある。

Queueクラスのコンストラクタで列挙を引数として、
複数要素で初期化できる。