using System.Collections.Generic;
public int Pass { get; set; }
public int Total { get; set; }
public static decimal GetMaxAvgPassRatio(List<Class> classes, int extraStudents)
var pQueue = new PriorityQueue<Class, decimal>();
Console.Write("class delta pass ratio: ");
decimal deltaPassRatio = getDeltaPassRatio(c);
Console.Write($"{deltaPassRatio.ToString("#0.00000")} ");
pQueue.Enqueue(c, -deltaPassRatio);
while (extraStudents > 0)
var c = pQueue.Dequeue();
Console.Write($"adding extra student to class: {JsonSerializer.Serialize(c)}");
decimal deltaPassRatio = getDeltaPassRatio(c);
Console.WriteLine($", new delta pass ratio: {deltaPassRatio.ToString("#0.00000")}");
pQueue.Enqueue(c, -deltaPassRatio);
decimal sumPassRatio = 0;
Console.Write("dequeueing priority queue: ");
var c = pQueue.Dequeue();
Console.Write($"{JsonSerializer.Serialize(c)} ");
sumPassRatio += (decimal)c.Pass / c.Total;
return sumPassRatio / classes.Count();
private static decimal getDeltaPassRatio(Class c)
return ((decimal)(c.Pass + 1) / (c.Total + 1)) - ((decimal)c.Pass / c.Total);
public static void Main()
var classes = new List<Class>()
new Class { Pass = 1, Total = 2 },
new Class { Pass = 3, Total = 5 },
new Class { Pass = 2, Total = 2 }
Console.WriteLine($"input: classes: {NestedListToString(classes, true)}; extra students: {extraStudents}");
Console.WriteLine($"output: {GetMaxAvgPassRatio(classes, extraStudents).ToString("#0.00000")}");
classes = new List<Class>()
new Class { Pass = 2, Total = 4 },
new Class { Pass = 3, Total = 9 },
new Class { Pass = 4, Total = 5 },
new Class { Pass = 2, Total = 10 }
Console.WriteLine($"input: classes: {NestedListToString(classes, true)}; extra students: {extraStudents}");
Console.WriteLine($"output: {GetMaxAvgPassRatio(classes, extraStudents).ToString("#0.00000")}");
public static string NestedListToString<T>
string openingBracket = $"{(asArray ? "[" : "{")}{(indented ? $"\n{new string('\t', level)}" : "")}";
string closingBracket = $"{(indented ? $"\n{new string('\t', level - 1)}" : "")}{(asArray ? "]" : "}")}";
string joinSeparator = $",{(indented ? $"\n{new string('\t', level)}" : "")}";
if (typeof(T).IsValueType || typeof(T) == typeof(String))
return $"{(asArray ? "[" : "{")}{string.Join(',', nestedList)}{(asArray ? "]" : "}")}";
else if (typeof(T).IsNested)
return $"{openingBracket}{string.Join(joinSeparator, nestedList.Select(i => JsonSerializer.Serialize(i)))}{closingBracket}";
var nestedListString = string.Join(joinSeparator,
nestedList.Select(nl => typeof(Program)
.GetMethod("NestedListToString")
.MakeGenericMethod(typeof(T).GetGenericArguments()[0])
.Invoke(null, new object[] { nl, asArray, indented, level + 1 })).ToList());
return $"{openingBracket}{nestedListString}{closingBracket}";