Advertisement
Guest User

Untitled

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