private static List <Legion> AddNewLegionInfo(List <Legion> legions, string[] input) { int lastActivity = Convert.ToInt32(input[0]); string legionName = input[1]; string soldierType = input[2]; int soldierCount = Convert.ToInt32(input[3]); if (!legions.Exists(l => l.Name.Equals(legionName))) { Legion newLegion = new Legion { Name = legionName, LastActivity = lastActivity, Soldiers = new Dictionary <string, long> { } }; legions.Add(newLegion); } Legion legion = legions.Where(l => l.Name.Equals(legionName)).First(); if (lastActivity > legion.LastActivity) { legion.LastActivity = lastActivity; } if (!legion.Soldiers.ContainsKey(soldierType)) { legion.Soldiers.Add(soldierType, 0); } legion.Soldiers[soldierType] += soldierCount; return(legions); }
public static void Main() { var n = int.Parse(Console.ReadLine()); var legions = new Dictionary <string, Legion>(); for (int i = 0; i < n; i++) { var line = Console.ReadLine() .Split(new char[] { '=', '-', '>', ':', ' ' }, StringSplitOptions.RemoveEmptyEntries); var lastActivity = int.Parse(line[0]); var legionName = line[1]; var soldierType = line[2]; var soldierCount = int.Parse(line[3]); if (!legions.ContainsKey(legionName)) { legions[legionName] = new Legion() { Activity = 0, SoldierType = new Dictionary <string, int>() }; } if (!legions[legionName].SoldierType.ContainsKey(soldierType)) { legions[legionName].SoldierType.Add(soldierType, soldierCount); if (legions[legionName].Activity < lastActivity) { legions[legionName].Activity = lastActivity; } } else { legions[legionName].SoldierType[soldierType] += soldierCount; if (legions[legionName].Activity < lastActivity) { legions[legionName].Activity = lastActivity; } } } var query = Console.ReadLine(); var queryActivity = 0; var querySoldierType = string.Empty; if (query.Contains("\\")) { var queries = query.Split('\\'); queryActivity = int.Parse(queries[0]); querySoldierType = queries[1]; var result = legions.Values[querySoldierType]; foreach (var item in result) { } //foreach (var legion in result.OrderByDescending(l => l.Value.SoldierType.Values.Sum())) //{ // if (legion.Value.Activity < queryActivity) // { // } //} } else { querySoldierType = query; foreach (var legion in legions.OrderByDescending(a => a.Value.Activity)) { if (legion.Value.SoldierType.ContainsKey(querySoldierType)) { Console.WriteLine($"{legion.Value.Activity} : {legion.Key}"); } } } }
public static void Main(string[] args) { List <Legion> legions = new List <Legion>(); var n = long.Parse(Console.ReadLine()); for (long i = 0; i < n; i++) { var inputArgs = Console.ReadLine().Split(new string[] { " = ", " -> ", ":" }, StringSplitOptions.RemoveEmptyEntries); long activity = long.Parse(inputArgs[0]); string legion = inputArgs[1]; string soldier = inputArgs[2]; long count = long.Parse(inputArgs[3]); if (!legions.Any(l => l.Name == legion)) { Legion currentLegion = new Legion() { Activity = activity, Name = legion, Soldiers = new Dictionary <string, long>() }; currentLegion.Soldiers.Add(soldier, count); legions.Add(currentLegion); } else { Legion currentLegion = legions.FirstOrDefault(l => l.Name == legion); if (currentLegion.Activity < activity) { currentLegion.Activity = activity; } if (!currentLegion.Soldiers.ContainsKey(soldier)) { currentLegion.Soldiers.Add(soldier, count); } else { currentLegion.Soldiers[soldier] += count; } } } var printArgs = Console.ReadLine().Split('\\'); if (printArgs.Length > 1) { long activity = long.Parse(printArgs[0]); string soldier = printArgs[1]; Dictionary <string, long> rem = new Dictionary <string, long>(); foreach (var legion in legions .Where(l => l.Activity < activity) .Where(l => l.Soldiers.ContainsKey(soldier))) { rem.Add(legion.Name, legion.Soldiers[soldier]); } foreach (var item in rem.OrderByDescending(k => k.Value)) { Console.WriteLine($"{item.Key} -> {item.Value}"); } } else { string soldier = printArgs[0]; Dictionary <string, long> rem = new Dictionary <string, long>(); foreach (var legion in legions.Where(l => l.Soldiers.ContainsKey(soldier))) { rem.Add(legion.Name, legion.Activity); } foreach (var item in rem.OrderByDescending(p => p.Value)) { Console.WriteLine($"{item.Value} -> {item.Key}"); } } }
static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); List <Legion> legions = new List <Legion>(); for (int i = 0; i < n; i++) { string input = Console.ReadLine(); string[] tokens = input.Split(new string[] { " = ", " -> ", ":" }, StringSplitOptions.RemoveEmptyEntries); int lastActivity = int.Parse(tokens[0]); string legionName = tokens[1]; string soldierType = tokens[2]; int soldiersCount = int.Parse(tokens[3]); if (legions.Select(l => l.LegionName).Contains(legionName)) { foreach (var item in legions.Where(l => l.LegionName == legionName)) { if (item.LastActivity < lastActivity) { item.LastActivity = lastActivity; } if (!item.SoldiersTypeData.ContainsKey(soldierType)) { item.SoldiersTypeData.Add(soldierType, 0); } item.SoldiersTypeData[soldierType] += soldiersCount; } } else { Legion legion = new Legion(legionName, lastActivity, soldierType, soldiersCount); legions.Add(legion); } } string[] printTokens = Console.ReadLine().Split('\\'); if (printTokens.Length == 2) { int maxActivity = int.Parse(printTokens[0]); string wantedSoldierType = printTokens[1]; foreach (var item in legions.Where(l => l.SoldiersTypeData.ContainsKey(wantedSoldierType)) .OrderByDescending(l => l.SoldiersTypeData[wantedSoldierType])) { if (item.LastActivity < maxActivity) { Console.WriteLine($"{item.LegionName} -> {item.SoldiersTypeData[wantedSoldierType]}"); } } } else { string wantedSoldierType = printTokens[0]; foreach (var item in legions.Where(l => l.SoldiersTypeData.ContainsKey(wantedSoldierType)) .OrderByDescending(l => l.LastActivity)) { Console.WriteLine($"{item.LastActivity} : {item.LegionName}"); } } }
static void Main(string[] args) { Dictionary <string, Legion> legions = new Dictionary <string, Legion>(); int N = int.Parse(Console.ReadLine()); List <string> types = new List <string>(); for (int i = 0; i < N; i++) { string[] input = Console.ReadLine().Split(new char[] { '=', '-', '>', ':' }, StringSplitOptions.RemoveEmptyEntries); long activity = long.Parse(input[0]); string legionName = input[1].Trim(); string soldierType = input[2].Trim(); long soldierAmount = long.Parse(input[3]); Legion curLeg = new Legion(); if (!legions.ContainsKey(legionName)) { curLeg.Activity = activity; curLeg.typeAndCount = new Dictionary <string, long>(); curLeg.typeAndCount.Add(soldierType, soldierAmount); legions.Add(legionName, curLeg); if (!types.Contains(soldierType)) { types.Add(soldierType); } } else { if (activity > legions[legionName].Activity) { legions[legionName].Activity = activity; } if (legions[legionName].typeAndCount.ContainsKey(soldierType)) { legions[legionName].typeAndCount[soldierType] = legions[legionName].typeAndCount[soldierType] + soldierAmount; } else { legions[legionName].typeAndCount.Add(soldierType, soldierAmount); } } } string outputType = Console.ReadLine(); Regex pat = new Regex(@"^\d+\\[^=:->]+$"); if (pat.IsMatch(outputType)) { string wantedType = outputType.Substring(outputType.IndexOf("\\") + 1); if (types.Contains(wantedType)) { Dictionary <string, Legion> prefixed = new Dictionary <string, Legion>(); long maxActivity = long.Parse(outputType.Substring(0, outputType.IndexOf("\\"))); foreach (var item in legions) { if (item.Value.typeAndCount.ContainsKey(wantedType)) { prefixed.Add(item.Key, item.Value); } } foreach (var leg in prefixed.OrderByDescending(x => x.Value.typeAndCount[wantedType])) { if (leg.Value.Activity < maxActivity && leg.Value.typeAndCount.ContainsKey(wantedType)) { Console.WriteLine(leg.Key + " -> " + leg.Value.typeAndCount[wantedType]); } } } } else { string wanted = outputType.Trim(); if (types.Contains(wanted)) { foreach (var leg in legions.OrderByDescending(x => x.Value.Activity)) { if (leg.Value.typeAndCount.ContainsKey(wanted)) { Console.WriteLine(leg.Value.Activity + " : " + leg.Key); } } } } }
static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); var legions = new Dictionary <string, Legion>(); string input = String.Empty; for (int i = 0; i < n; i++) { input = Console.ReadLine(); string[] tokens = input.Split(new[] { " = ", " -> ", ":" }, StringSplitOptions.RemoveEmptyEntries); string legionName = tokens[1]; int activity = int.Parse(tokens[0]); string soldierType = tokens[2]; long soldierCount = long.Parse(tokens[3]); if (!legions.ContainsKey(legionName)) { legions[legionName] = new Legion(); legions[legionName].Activity = activity; legions[legionName].Soldiers.Add(soldierType, soldierCount); } else { if (legions[legionName].Activity < activity) { legions[legionName].Activity = activity; } if (!legions[legionName].Soldiers.ContainsKey(soldierType)) { legions[legionName].Soldiers.Add(soldierType, soldierCount); } else { legions[legionName].Soldiers[soldierType] += soldierCount; } } } input = Console.ReadLine(); Regex patternwithActivity = new Regex(@"\d+\\."); if (patternwithActivity.IsMatch(input)) { string[] arr = input.Split('\\'); int aboveAllowedActivity = int.Parse(arr[0]); string type = arr[1]; var soldiers = legions.Where(x => x.Value.Activity < aboveAllowedActivity && x.Value.Soldiers.ContainsKey(type)). OrderByDescending(x => x.Value.Soldiers[type]); foreach (var soldier in soldiers) { Console.WriteLine($"{soldier.Key} -> {soldier.Value.Soldiers[type]}"); } } else { var soldiers = legions.Where(x => x.Value.Soldiers.ContainsKey(input)) .OrderByDescending(x => x.Value.Activity); foreach (var soldier in soldiers) { Console.WriteLine($"{soldier.Value.Activity} : {soldier.Key}"); } } }