結果

問題 No.714 回転寿司屋のシミュレート
ユーザー Wataru MaedaWataru Maeda
提出日時 2018-10-10 22:27:52
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 4,792 bytes
コンパイル時間 1,160 ms
コンパイル使用メモリ 113,408 KB
実行使用メモリ 23,808 KB
最終ジャッジ日時 2024-04-20 19:31:29
合計ジャッジ時間 3,945 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
21,888 KB
testcase_01 AC 42 ms
21,888 KB
testcase_02 AC 43 ms
21,888 KB
testcase_03 AC 45 ms
22,144 KB
testcase_04 AC 46 ms
22,272 KB
testcase_05 AC 50 ms
22,272 KB
testcase_06 AC 53 ms
22,528 KB
testcase_07 AC 64 ms
23,296 KB
testcase_08 AC 76 ms
23,808 KB
testcase_09 AC 53 ms
23,424 KB
testcase_10 AC 50 ms
22,912 KB
testcase_11 AC 53 ms
22,272 KB
testcase_12 AC 51 ms
22,528 KB
testcase_13 AC 52 ms
22,528 KB
testcase_14 AC 50 ms
22,400 KB
testcase_15 AC 51 ms
22,656 KB
testcase_16 AC 51 ms
22,400 KB
testcase_17 AC 50 ms
23,040 KB
testcase_18 AC 51 ms
22,528 KB
testcase_19 AC 49 ms
22,400 KB
testcase_20 AC 51 ms
22,656 KB
testcase_21 AC 51 ms
22,528 KB
testcase_22 AC 47 ms
22,528 KB
testcase_23 AC 47 ms
22,400 KB
testcase_24 AC 47 ms
22,400 KB
testcase_25 AC 46 ms
22,528 KB
testcase_26 AC 48 ms
22,400 KB
testcase_27 AC 47 ms
22,272 KB
testcase_28 AC 46 ms
22,400 KB
testcase_29 AC 47 ms
22,272 KB
testcase_30 AC 41 ms
21,760 KB
testcase_31 AC 42 ms
21,888 KB
testcase_32 AC 43 ms
21,504 KB
testcase_33 AC 42 ms
21,760 KB
testcase_34 AC 42 ms
21,888 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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

namespace Problem714
{
    /// <summary>
    /// 寿司
    /// </summary>
    class Sushi
    {
        public string Name { get; }
        public Sushi(string name) { Name = name; }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            var s = obj as Sushi;
            if (s == null) return false;
            return Name == s.Name;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(Name);
        }
    }

    /// <summary>
    /// お客
    /// </summary>
    class Customer {
        public int SeatNum { get; protected set; }
        Dictionary<Sushi, int> _WishList;
        public IReadOnlyDictionary<Sushi, int> WishList => _WishList;
        private Customer() { }

        public bool EatIfWished(Sushi s)
        {
            if (!_WishList.ContainsKey(s)) return false;
            if (_WishList[s] == 0) return false;
            _WishList[s]--;
            return true;
        }
        public static Customer CreateFromStrings(params string[] strs)
        {
            var c = new Customer
            {
                SeatNum = int.Parse(strs[1]),
            };
            c._WishList = strs.Skip(2).Select(x => new Sushi(x)).GroupBy(x=>x).ToDictionary(x=>x.Key, x=>x.Count());
            return c;
        }
    }

    /// <summary>
    /// 回転寿司屋さん
    /// </summary>
    class RotarySushiBar
    {
        SortedList<int, Customer> _Customers = new SortedList<int, Customer>();
        public IReadOnlyList<Customer> Customers => _Customers.Values.ToList().AsReadOnly();

        public void AddCustomer(Customer c)
        {
            _Customers[c.SeatNum] = c;
        }

        public void RemoveCustomer(int seatNum)
        {
            _Customers.Remove(seatNum);
        }

        public Customer RoundSushi(Sushi s)
        {
            foreach (var c in _Customers.Values)
            {
                var ret = c.EatIfWished(s);
                if (ret)
                {
                    // 食べた!
                    return c;
                }
            }
            return null;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                DoTest();
                return;
            }

            var rsb = new RotarySushiBar();

            var n = int.Parse(Console.ReadLine());
            for (var i=0; i<n; i++)
            {
                var line = Console.ReadLine().Split(' ');
                switch(line[0])
                {
                    case "0":
                        rsb.AddCustomer(Customer.CreateFromStrings(line));
                        break;
                    case "1":
                        var c = rsb.RoundSushi(new Sushi(line[1]));
                        Console.WriteLine(c?.SeatNum ?? -1);
                        break;
                    case "2":
                        rsb.RemoveCustomer(int.Parse(line[1]));
                        break;
                }
            }
        }

        static void Assert<T>(T expected, T actual)
        {
            if (expected != null && actual != null && !expected.Equals(actual))
            {
                throw new Exception($"expected=({expected.GetType().Name}){expected}, actual=({actual.GetType().Name}){actual}");
            }
        }

        static void DoTest()
        {
            var rsb = new RotarySushiBar();

            // 客がいないので、寿司を流してもだれも取らない
            Assert(null, rsb.RoundSushi(new Sushi("toro")));
            Assert(null, rsb.RoundSushi(new Sushi("unagi")));
            // 14番席に客が座る
            rsb.AddCustomer(Customer.CreateFromStrings("0 14 4 maguroakami hotatekai unagi maguroakami".Split(' ')));
            // 寿司を流す
            Assert(null, rsb.RoundSushi(new Sushi("saamonnmottu")));  // 食べない
            Assert(14, rsb.RoundSushi(new Sushi("hotatekai"))?.SeatNum);      // 食べる
            Assert(null, rsb.RoundSushi(new Sushi("hotatekai"))?.SeatNum);      // 食べない
            // 15番席に客が座る
            rsb.AddCustomer(Customer.CreateFromStrings("0 15 3 maguroakami saamonnmottu hotatekai".Split(' ')));
            // 寿司を流す
            Assert(15, rsb.RoundSushi(new Sushi("hotatekai"))?.SeatNum);  // 食べる
            Assert(15, rsb.RoundSushi(new Sushi("saamonnmottu"))?.SeatNum);      // 食べる
            Assert(14, rsb.RoundSushi(new Sushi("unagi"))?.SeatNum);      // 食べる

            Console.WriteLine("Test all OK!");
            Console.ReadKey();
        }
    }
}
0