Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace TechFundamentalsExam
  6. {
  7.     public sealed class Legion
  8.     {
  9.         public Legion(string name, long activity)
  10.         {
  11.             this.Name = name;
  12.             this.Activity = activity;
  13.             this.Soldiers = new Dictionary<string, long>();
  14.         }
  15.  
  16.         public string Name { get; private set; }
  17.  
  18.         public long Activity { get; set; }
  19.  
  20.         public Dictionary<string, long> Soldiers { get; set; }
  21.  
  22.         public override string ToString()
  23.         {
  24.             return $"{this.Activity} : {this.Name}";
  25.         }
  26.     }
  27.  
  28.     class Startup
  29.     {
  30.         static void Main(string[] args)
  31.         {
  32.             int n = int.Parse(Console.ReadLine());
  33.             var legions = new Dictionary<string, Legion>();
  34.  
  35.             for (int i = 0; i < n; i++)
  36.             {
  37.                 string[] input = Console.ReadLine().Split(new[] { '=', ' ', ':', '-', '>' }, StringSplitOptions.RemoveEmptyEntries);
  38.                 long lastActivity = long.Parse(input[0]);
  39.                 string legionName = input[1];
  40.                 string soldierType = input[2];
  41.                 long soldierCount = long.Parse(input[3]);
  42.  
  43.                 if (legions.ContainsKey(legionName))
  44.                 {
  45.                     if (legions[legionName].Soldiers.ContainsKey(soldierType))
  46.                     {
  47.                         legions[legionName].Soldiers[soldierType] += soldierCount;
  48.                     }
  49.                     else
  50.                     {
  51.                         legions[legionName].Soldiers.Add(soldierType, soldierCount);
  52.                     }
  53.  
  54.                     if (legions[legionName].Activity < lastActivity)
  55.                     {
  56.                         legions[legionName].Activity = lastActivity;
  57.                     }
  58.                 }
  59.                 else
  60.                 {
  61.                     legions.Add(legionName, new Legion(legionName, lastActivity));
  62.                     legions[legionName].Soldiers.Add(soldierType, soldierCount);
  63.                 }
  64.             }
  65.  
  66.             string[] outputFormat = Console.ReadLine().Split(new[] { '\\' }, StringSplitOptions.None);
  67.  
  68.             if (outputFormat.Length > 1)
  69.             {
  70.                 long activity = long.Parse(outputFormat[0]);
  71.                 string soldierType = outputFormat[1];
  72.                 var found = new Dictionary<string, long>();
  73.  
  74.                 foreach (var legion in legions)
  75.                 {
  76.                     if (legion.Value.Activity >= activity)
  77.                     {
  78.                         continue;
  79.                     }
  80.  
  81.                     foreach (var soldier in legion.Value.Soldiers)
  82.                     {
  83.                         if (soldier.Key == soldierType)
  84.                         {
  85.                             found.Add(legion.Key, soldier.Value);
  86.                         }
  87.                     }
  88.                 }
  89.  
  90.                 foreach (var item in found.OrderByDescending(x => x.Value))
  91.                 {
  92.                     Console.WriteLine($"{item.Key} -> {item.Value}");
  93.                 }
  94.             }
  95.             else
  96.             {
  97.                 var foundWithType = legions.Where(x => x.Value.Soldiers.Keys.Contains(outputFormat[0]));
  98.  
  99.                 foreach (var soldier in foundWithType.OrderByDescending(x => x.Value.Activity))
  100.                 {
  101.                     Console.WriteLine(soldier.Value);
  102.                 }
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement