Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 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. if (ssoldier.Value.OrderByDescending(x => x.Key).First().Key < aactivity)
  67. {
  68. foreach (var task in ssoldier.Value)
  69. soldierNum.Add(task.Value);
  70. if (soldierNum.Sum() == 0)
  71. {
  72. continue;
  73. }
  74. else
  75. {
  76. if (resultToPrint.ContainsKey(llegion.Key) == false)
  77. {
  78. resultToPrint.Add(llegion.Key, soldierNum.Sum());
  79. }
  80. else
  81. {
  82. resultToPrint[llegion.Key] = soldierNum.Sum();
  83. }
  84. }
  85. }
  86.  
  87. }
  88. }
  89.  
  90. }
  91.  
  92. foreach (var item in resultToPrint.OrderByDescending(x=>x.Value))
  93. {
  94. Console.WriteLine($"{item.Key} -> {item.Value}");
  95. }
  96. }
  97. else
  98. {
  99. Dictionary<string, long> resultForPrint = new Dictionary<string, long>();
  100. string ssoldierType = command[0];
  101. foreach (var llegion in legions.Where(x=> x.Value.ContainsKey(ssoldierType)))
  102.  
  103. {
  104. long lastActivity=-1;
  105. foreach (var ssoldier in llegion.Value)
  106. {
  107. foreach (var task in ssoldier.Value)
  108. {
  109. if (task.Key > lastActivity)
  110. {
  111. lastActivity = task.Key;
  112. }
  113. }
  114. }
  115. if (resultForPrint.ContainsKey(llegion.Key) == false)
  116. {
  117. resultForPrint.Add(llegion.Key, lastActivity);
  118. }
  119. else
  120. {
  121. resultForPrint[llegion.Key] = lastActivity;
  122. }
  123.  
  124.  
  125. }
  126. foreach (var item in resultForPrint.OrderByDescending(x=>x.Value))
  127. {
  128. Console.WriteLine($"{item.Value} : {item.Key}");
  129. }
  130. }
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement