結果
問題 | No.714 回転寿司屋のシミュレート |
ユーザー |
|
提出日時 | 2018-10-10 22:28:20 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 70 ms / 2,000 ms |
コード長 | 4,792 bytes |
コンパイル時間 | 1,074 ms |
コンパイル使用メモリ | 121,964 KB |
実行使用メモリ | 23,936 KB |
最終ジャッジ日時 | 2024-10-12 17:22:45 |
合計ジャッジ時間 | 3,906 ms |
ジャッジサーバーID (参考情報) |
judge / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 35 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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();}}}