Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace HornetArmada
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             // input will be in the format:
  13.             // { lastActivity} = { legionName} -> { soldierType}:{ soldierCount}
  14.             Dictionary<string, Dictionary<string, Dictionary<long, long>>> legions = new Dictionary<string, Dictionary<string, Dictionary<long, long>>>();
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 string[] input = Console.ReadLine()
  18.                     .Split(" :=->".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
  19.                     .ToArray();
  20.  
  21.                 string legionType = input[1];
  22.                 long lastActivity = long.Parse(input[0]);
  23.                 string soldierType = input[2];
  24.                 long soldiersNumber = long.Parse(input[3]);
  25.  
  26.                 Dictionary<long, long> activities = new Dictionary<long, long>();
  27.                 Dictionary<string, Dictionary<long, long>> soldiersTypes =
  28.                         new Dictionary<string, Dictionary<long, long>>();
  29.                 if (legions.ContainsKey(legionType) == false)
  30.                 {
  31.                     activities.Add(lastActivity, soldiersNumber);
  32.                     soldiersTypes.Add(soldierType, activities);
  33.                     legions.Add(legionType, soldiersTypes);
  34.                 }
  35.                 else if (legions[legionType].ContainsKey(soldierType) == false)
  36.                 {
  37.                     activities.Add(lastActivity, soldiersNumber);
  38.                     legions[legionType].Add(soldierType, activities);
  39.                 }
  40.                 else if (legions[legionType][soldierType].ContainsKey(lastActivity) == false)
  41.                 {
  42.                     legions[legionType][soldierType].Add(lastActivity, soldiersNumber);
  43.                 }
  44.                 else
  45.                 {
  46.                     legions[legionType][soldierType][lastActivity]+= soldiersNumber;
  47.                 }
  48.             }
  49.  
  50.             string[] command = Console.ReadLine()
  51.                     .Split('\\')
  52.                 .ToArray();
  53.             //printing the result
  54.            
  55.             if (command.Length == 2)
  56.             {
  57.                 long aactivity = long.Parse(command[0]);
  58.                 string ssoldierType = command[1];
  59.                 Dictionary<string, long> resultToPrint = new Dictionary<string, long>();
  60.                
  61.                 foreach  (var llegion in legions)
  62.                 {
  63.                     foreach (var ssoldier in llegion.Value.Where(x => x.Key==ssoldierType))
  64.                     {
  65.                         { List<long> soldierNum = new List<long>();
  66.                             foreach (var task in ssoldier.Value.Where(x => x.Key < aactivity))
  67.                                 soldierNum.Add(task.Value);
  68.                             if (soldierNum.Sum() == 0)
  69.                             {
  70.                                 continue;
  71.                             }
  72.                             else
  73.                             {
  74.                                 if (resultToPrint.ContainsKey(llegion.Key) == false)
  75.                                 {
  76.                                     resultToPrint.Add(llegion.Key, soldierNum.Sum());
  77.                                 }
  78.                                 else
  79.                                 {
  80.                                     resultToPrint[llegion.Key] = soldierNum.Sum();
  81.                                 }
  82.                                
  83.                             }
  84.                         }
  85.                     }
  86.  
  87.                 }
  88.  
  89.                 foreach (var item in resultToPrint.OrderByDescending(x=>x.Value))
  90.                 {
  91.                     Console.WriteLine($"{item.Key} -> {item.Value}");
  92.                 }
  93.             }
  94.             else
  95.             {
  96.                 Dictionary<string, long> resultForPrint = new Dictionary<string, long>();
  97.                 string ssoldierType = command[0];
  98.                 foreach (var llegion in legions.Where(x=> x.Value.ContainsKey(ssoldierType)))
  99.                    
  100.                 {
  101.                 long lastActivity=-1;
  102.                     foreach (var ssoldier in llegion.Value)
  103.                     {
  104.                         foreach (var task in ssoldier.Value)
  105.                         {
  106.                             if (task.Key > lastActivity)
  107.                             {
  108.                                 lastActivity = task.Key;
  109.                             }
  110.                         }
  111.                     }
  112.                     if (resultForPrint.ContainsKey(llegion.Key) == false)
  113.                     {
  114.                         resultForPrint.Add(llegion.Key, lastActivity);
  115.                     }
  116.                     else
  117.                     {
  118.                         resultForPrint[llegion.Key] = lastActivity;
  119.                     }
  120.                        
  121.  
  122.                 }
  123.                 foreach (var item in resultForPrint.OrderByDescending(x=>x.Value))
  124.                 {
  125.                     Console.WriteLine($"{item.Value} : {item.Key}");
  126.                 }
  127.             }
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement