Advertisement
Prohause

Greedy time

Oct 10th, 2018
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace test
  6. {
  7. internal class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. long bagCapacity = long.Parse(Console.ReadLine());
  12. string[] itemQuality = Console.ReadLine().Split(new []{' ','\t','\n'}, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
  13.  
  14. Dictionary<string, Dictionary<string, long>> summuryWin = new Dictionary<string, Dictionary<string, long>>
  15. {
  16. {"GOLD", new Dictionary<string, long>()},
  17. {"GEM", new Dictionary<string, long>()},
  18. {"CASH", new Dictionary<string, long>()}
  19. };
  20.  
  21.  
  22. long currentCapacity = 0;
  23.  
  24. for (int i = 0; i < itemQuality.Length; i++)
  25. {
  26. string upperInput = itemQuality[i].ToUpper();
  27.  
  28. if (i % 2 == 0)
  29. {
  30. if (upperInput.Length <= 2)
  31. {
  32. continue;
  33. }
  34. if (upperInput == "GOLD")
  35. {
  36. var quantity = long.Parse(itemQuality[i + 1]);
  37.  
  38. if (currentCapacity + quantity > bagCapacity)
  39. {
  40. break;
  41. }
  42.  
  43. currentCapacity += quantity;
  44.  
  45.  
  46. if (!summuryWin["GOLD"].ContainsKey(itemQuality[i]))
  47. {
  48. summuryWin["GOLD"].Add(itemQuality[i], 0);
  49. }
  50. summuryWin["GOLD"][itemQuality[i]] += quantity;
  51.  
  52. }
  53. else if (upperInput.Length == 3)
  54. {
  55. var quantity = long.Parse(itemQuality[i + 1]);
  56. if (summuryWin["GEM"].Values.Sum() >= summuryWin["CASH"].Values.Sum() + quantity)
  57. {
  58.  
  59. if (currentCapacity + quantity > bagCapacity)
  60. {
  61. break;
  62. }
  63.  
  64. currentCapacity += quantity;
  65.  
  66.  
  67. if (!summuryWin["CASH"].ContainsKey(itemQuality[i]))
  68. {
  69. summuryWin["CASH"].Add(itemQuality[i], 0);
  70. }
  71. summuryWin["CASH"][itemQuality[i]] += quantity;
  72. }
  73. }
  74. else if (upperInput.EndsWith("GEM")&&upperInput.Length>=4)
  75. {
  76. var quantity = long.Parse(itemQuality[i + 1]);
  77. if (summuryWin["GOLD"].Values.Sum() >= summuryWin["GEM"].Values.Sum() + quantity)
  78. {
  79. if (currentCapacity+quantity > bagCapacity)
  80. {
  81. break;
  82. }
  83. currentCapacity += quantity;
  84.  
  85.  
  86. if (!summuryWin["GEM"].ContainsKey(itemQuality[i]))
  87. {
  88. summuryWin["GEM"].Add(itemQuality[i], 0);
  89. }
  90. summuryWin["GEM"][itemQuality[i]] += quantity;
  91.  
  92. }
  93. }
  94. }
  95. }
  96.  
  97. foreach (var type in summuryWin.OrderByDescending(x => x.Value.Values.Sum()))
  98. {
  99. if (type.Key == "GOLD")
  100. {
  101. if (type.Value.Count == 0)
  102. {
  103. continue;
  104. }
  105.  
  106. Console.WriteLine($"<Gold> ${type.Value.Values.Sum()}");
  107.  
  108. foreach (var item in type.Value.OrderByDescending(x => x.Key).ThenBy(x => x.Value))
  109. {
  110. Console.WriteLine($"##{item.Key} - {item.Value}");
  111. }
  112. }
  113. else if (type.Key == "GEM")
  114. {
  115. if (type.Value.Count == 0)
  116. {
  117. continue;
  118. }
  119.  
  120. Console.WriteLine($"<Gem> ${type.Value.Values.Sum()}");
  121.  
  122. foreach (var item in type.Value.OrderByDescending(x => x.Key).ThenBy(x => x.Value))
  123. {
  124. Console.WriteLine($"##{item.Key} - {item.Value}");
  125. }
  126. }
  127. else if (type.Key == "CASH")
  128. {
  129. if (type.Value.Count == 0)
  130. {
  131. continue;
  132. }
  133.  
  134. Console.WriteLine($"<Cash> ${type.Value.Values.Sum()}");
  135.  
  136. foreach (var item in type.Value.OrderByDescending(x => x.Key).ThenBy(x => x.Value))
  137. {
  138. Console.WriteLine($"##{item.Key} - {item.Value}");
  139. }
  140. }
  141. }
  142. }
  143.  
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement