using System;
using System.Collections.Generic;
using System.Linq;
namespace Problem714
{
///
/// 寿司
///
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);
}
}
///
/// お客
///
class Customer {
public int SeatNum { get; protected set; }
Dictionary _WishList;
public IReadOnlyDictionary 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;
}
}
///
/// 回転寿司屋さん
///
class RotarySushiBar
{
SortedList _Customers = new SortedList();
public IReadOnlyList 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(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();
}
}
}