AOJ本の読書メモ   AOJ    次のAOJの問題へ    前のAOJの問題へ

ALDS1_3_B: Queue


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

// Q008 キュー https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_B&lang=jp
class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("5 100");
            WillReturn.Add("p1 150");
            WillReturn.Add("p2 80");
            WillReturn.Add("p3 200");
            WillReturn.Add("p4 350");
            WillReturn.Add("p5 20");
            //p2 180
            //p5 400
            //p1 450
            //p3 550
            //p4 800
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct ProcessInfoDef
    {
        internal string Name;
        internal int Time;
    }

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

        int[] wkArr = InputList[0].Split(' ').Select(X => int.Parse(X)).ToArray();
        int Q = wkArr[1];

        var ProcessInfoList = new List<ProcessInfoDef>();
        ProcessInfoDef WillEnqueue;

        var Que = new Queue<ProcessInfoDef>();
        foreach (string EachStr in InputList.Skip(1)) {
            string[] SplitArr = EachStr.Split(' ');
            WillEnqueue.Name = SplitArr[0];
            WillEnqueue.Time = int.Parse(SplitArr[1]);
            Que.Enqueue(WillEnqueue);
        }

        int CurrTime = 0;
        while (Que.Count > 0) {
            ProcessInfoDef Dequeued = Que.Dequeue();

            WillEnqueue.Name = Dequeued.Name;
            WillEnqueue.Time = Dequeued.Time;

            int ElapsedTime = Math.Min(Q, WillEnqueue.Time);
            WillEnqueue.Time -= ElapsedTime;
            CurrTime += ElapsedTime;

            if (WillEnqueue.Time == 0) {
                Console.WriteLine("{0} {1}", WillEnqueue.Name, CurrTime);
                continue;
            }
            Que.Enqueue(WillEnqueue);
        }
    }
}


解説

Queueジェネリックを使ってます。