Advertisement
Militsa

02. Hornet Armada

Dec 8th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _02.Hornet_Armada
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var legionWithSoldiers = new Dictionary<string, Dictionary<string, long>>();
  14.             var legionWithActivity = new Dictionary<string, int>();
  15.  
  16.             int n = int.Parse(Console.ReadLine());
  17.             for (int i = 0; i < n; i++)
  18.             {
  19.                 var input = Console.ReadLine();
  20.                 var parts = input.Split(new string[] { " -> " }, StringSplitOptions.None);
  21.                 var  firstTwo = (parts[0].Split(new string[] {" = " },StringSplitOptions.None));
  22.                 var secondTwo = (parts[1].Split(':'));
  23.  
  24.                 int lastActivity = int.Parse(firstTwo[0]);
  25.                 string legion = firstTwo[1];
  26.                 string soldierType = secondTwo[0];
  27.                 int soldierCount = int.Parse(secondTwo[1]);
  28.  
  29.                 if (!legionWithSoldiers.ContainsKey(legion))
  30.                 {
  31.                     legionWithSoldiers[legion] = new Dictionary<string, long>();
  32.                     legionWithActivity[legion] = lastActivity;
  33.                 }
  34.  
  35.                 if (!legionWithSoldiers[legion].ContainsKey(soldierType))
  36.                 {
  37.                     legionWithSoldiers[legion][soldierType] = 0;
  38.                 }
  39.                 legionWithSoldiers[legion][soldierType] += soldierCount;
  40.  
  41.                 if(lastActivity> legionWithActivity[legion])
  42.                 {
  43.                     legionWithActivity[legion] = lastActivity;
  44.                 }
  45.             }
  46.  
  47.             string task = Console.ReadLine();
  48.             if (task.Contains('\\'))
  49.             {
  50.                 var tokens = task.Split('\\');
  51.                 int maxActivity = int.Parse(tokens[0]);
  52.                 var soldierType = tokens[1];
  53.  
  54.                 var legionsWithLowerActivity = legionWithSoldiers
  55.                     .Where(x => legionWithActivity[x.Key] < maxActivity)
  56.                     .Where(x=>x.Value.ContainsKey(soldierType))
  57.                     .OrderByDescending(x=>x.Value[soldierType])
  58.                     .ToDictionary(x=>x.Key,x=>x.Value);
  59.  
  60.                 foreach (var item in legionsWithLowerActivity)
  61.                 {
  62.                     Console.WriteLine("{0} -> {1}", item.Key, item.Value[soldierType]);
  63.                 }
  64.             }
  65.             else
  66.             {
  67.                 string st = task;
  68.  
  69.                 var sortedLegions = legionWithSoldiers
  70.                     .Where(x => x.Value.ContainsKey(st))
  71.                     .OrderByDescending(x => legionWithActivity[x.Key])
  72.                     .ToDictionary(x => x.Key, x => x.Value);
  73.  
  74.                 foreach (var item in sortedLegions)
  75.                 {
  76.                     Console.WriteLine("{0} : {1}", legionWithActivity[item.Key], item.Key);
  77.                 }
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement