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

ABC024-B 自動ドア


問題へのリンク


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("5 10");
            WillReturn.Add("20");
            WillReturn.Add("100");
            WillReturn.Add("105");
            WillReturn.Add("217");
            WillReturn.Add("314");
            //45
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10 10");
            WillReturn.Add("1");
            WillReturn.Add("2");
            WillReturn.Add("3");
            WillReturn.Add("4");
            WillReturn.Add("5");
            WillReturn.Add("6");
            WillReturn.Add("7");
            WillReturn.Add("8");
            WillReturn.Add("9");
            WillReturn.Add("10");
            //19
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10 100000");
            WillReturn.Add("3");
            WillReturn.Add("31");
            WillReturn.Add("314");
            WillReturn.Add("3141");
            WillReturn.Add("31415");
            WillReturn.Add("314159");
            WillReturn.Add("400000");
            WillReturn.Add("410000");
            WillReturn.Add("500000");
            WillReturn.Add("777777");
            //517253
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int T = wkArr[1];

        int[] AArr = InputList.Skip(1).Select(pX => int.Parse(pX)).ToArray();

        var InsSortedList = new SortedList<int, int>();
        foreach (int EachA in AArr) {
            if (InsSortedList.ContainsKey(EachA) == false) {
                InsSortedList[EachA] = 0;
            }
            if (InsSortedList.ContainsKey(EachA + T) == false) {
                InsSortedList[EachA + T] = 0;
            }
            InsSortedList[EachA]++;
            InsSortedList[EachA + T]--;
        }

        int[] KeysArr = InsSortedList.Keys.ToArray();
        for (int I = 1; I <= KeysArr.GetUpperBound(0); I++) {
            int CurrKey = KeysArr[I];
            int PrevKey = KeysArr[I - 1];
            InsSortedList[CurrKey] += InsSortedList[PrevKey];
        }

        int Answer = 0;
        for (int I = 0; I <= KeysArr.GetUpperBound(0) - 1; I++) {
            int CurrKey = KeysArr[I];
            int NextKey = KeysArr[I + 1];

            if (InsSortedList[CurrKey] <= 0) continue;
            Answer += NextKey - CurrKey;
        }
        Console.WriteLine(Answer);
    }
}


解説

SortedListで、座標圧縮しつつ、いもす法を使ってます。