Advertisement
Prohause

Problem 4 – Iron Girder

Aug 29th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace zada4a4
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var allCities = new Dictionary<string, KeyValuePair<int, long>>();
  12. var input = string.Empty;
  13. while (!(input = Console.ReadLine()).Equals("Slide rule"))
  14. {
  15. var tokens = input.Split(new[] { ":", "->" }, StringSplitOptions.RemoveEmptyEntries);
  16. var city = tokens[0];
  17. var populationCount = int.Parse(tokens[2]);
  18.  
  19. if (tokens[1].Equals("ambush"))
  20. {
  21. if (allCities.ContainsKey(city))
  22. {
  23. var currentCount = allCities[city].Value;
  24. currentCount -= populationCount;
  25. if (currentCount<0)
  26. {
  27. currentCount = 0;
  28. }
  29. allCities[city] = new KeyValuePair<int, long>(0, currentCount);
  30. }
  31. }
  32. else
  33. {
  34. var time = int.Parse(tokens[1]);
  35. if (!allCities.ContainsKey(city))
  36. {
  37. allCities.Add(city, new KeyValuePair<int, long>(time, populationCount));
  38. }
  39. else
  40. {
  41. var currentCount = allCities[city].Value;
  42. var currentTime = allCities[city].Key;
  43.  
  44. currentCount += populationCount;
  45. if (currentTime == 0 || currentTime > time)
  46. {
  47. currentTime = time;
  48. }
  49.  
  50. allCities[city] = new KeyValuePair<int, long>(currentTime, currentCount);
  51. }
  52. }
  53. }
  54.  
  55. foreach (var city in allCities.OrderBy(x => x.Value.Key).ThenBy(x => x.Key))
  56. {
  57. if (allCities[city.Key].Key == 0 || allCities[city.Key].Value == 0)
  58. {
  59. continue;
  60. }
  61. Console.WriteLine($"{city.Key} -> Time: {city.Value.Key} -> Passengers: {city.Value.Value}");
  62.  
  63. }
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement