典型問題    次の典型問題へ    前の典型問題へ

典型アルゴリズム問題集 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("3");
            WillReturn.Add("1 5");
            WillReturn.Add("4 6");
            WillReturn.Add("6 8");
            //2
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct ABInfoDef
    {
        internal int A;
        internal int B;
    }

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

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

        var ABInfoList = new List<ABInfoDef>();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            ABInfoDef WillAdd;
            WillAdd.A = wkArr[0];
            WillAdd.B = wkArr[1];
            ABInfoList.Add(WillAdd);
        }

        // 終了日の昇順にソートする
        var Query = ABInfoList.OrderBy(pX => pX.B);

        int CurrDay = 0;
        int Answer = 0;
        foreach (var EachItem in Query) {
            if (EachItem.A <= CurrDay) continue;

            CurrDay = EachItem.B;
            Answer++;
        }
        Console.WriteLine(Answer);
    }
}


解説

終了日の昇順にソートして、可能なタスクかを判定してます。


類題

ABC131-D Megalomania