shady_obeyd

04.Hornet Armada

Oct 24th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _04.HornetArmada
  6. {
  7.     class HornetArmada
  8.     {
  9.         static void Main()
  10.         {
  11.             Dictionary<string, long> legionActivities = new Dictionary<string, long>();
  12.             Dictionary<string, Dictionary<string, long>> legionSoldiers = new Dictionary<string, Dictionary<string, long>>();
  13.  
  14.             int n = int.Parse(Console.ReadLine());
  15.  
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 string[] inputTokens = Console.ReadLine().Split(new char[] { '=', '-', '>', ':', ' ' }, StringSplitOptions.RemoveEmptyEntries);
  19.  
  20.                 int lastActivity = int.Parse(inputTokens[0]);
  21.                 string legionName = inputTokens[1];
  22.                 string soldierType = inputTokens[2];
  23.                 int soldierCount = int.Parse(inputTokens[3]);
  24.  
  25.                 if (!legionActivities.ContainsKey(legionName))
  26.                 {
  27.                     legionActivities.Add(legionName, lastActivity);
  28.                     legionSoldiers.Add(legionName, new Dictionary<string, long>());
  29.                 }
  30.  
  31.                 if (!legionSoldiers[legionName].ContainsKey(soldierType))
  32.                 {
  33.                     legionSoldiers[legionName][soldierType] = 0;
  34.                 }
  35.  
  36.                 if (legionActivities[legionName] < lastActivity)
  37.                 {
  38.                     legionActivities[legionName] = lastActivity;
  39.                 }
  40.  
  41.                 legionSoldiers[legionName][soldierType] += soldierCount;
  42.             }
  43.  
  44.             string[] tokens = Console.ReadLine().Split('\\');
  45.  
  46.             if (tokens.Length == 1)
  47.             {
  48.  
  49.                 string soldierType = tokens[0];
  50.  
  51.                 foreach (KeyValuePair<string, long> activity in legionActivities
  52.                     .OrderByDescending(a => a.Value))
  53.                 {
  54.                     if (legionSoldiers[activity.Key].ContainsKey(soldierType))
  55.                     {
  56.                         Console.WriteLine($"{activity.Value} : {activity.Key}");
  57.                     }
  58.                 }
  59.             }
  60.             else
  61.             {
  62.                 long activity = long.Parse(tokens[0]);
  63.                 string soldierType = tokens[1];
  64.  
  65.                 foreach (var soldier in legionSoldiers
  66.                     .Where(s => s.Value.ContainsKey(soldierType))
  67.                     .OrderByDescending(s => s.Value[soldierType]))
  68.                 {
  69.  
  70.                     if (legionActivities[soldier.Key] < activity)
  71.                     {
  72.                         Console.WriteLine($"{soldier.Key} -> {soldier.Value[soldierType]}");
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.     }
  78. }
Add Comment
Please, Sign In to add comment