Advertisement
Prohause

Greedy time 60/100

Oct 11th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 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. private static long goldammountAll;
  10. private static long gemammountAll;
  11. private static long cashammountAll;
  12. private static List<string> storage;
  13. private static string currtype;
  14. private static long currammount;
  15. private static string[] helparray;
  16. private static Dictionary<string, Dictionary<string, long>> dict;
  17. private static Dictionary<string, long> output;
  18.  
  19. private static void Main(string[] args)
  20. {
  21. var inputammount = long.Parse(Console.ReadLine());
  22. dict = new Dictionary<string, Dictionary<string, long>>();
  23. output = new Dictionary<string, long>();
  24. var input = Console.ReadLine().Split(new[] { ' ', '\t', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToArray();//Split() връща масив от отделни букви!!!
  25. //Провери в Google какво означава White Space
  26. FillInput(input);
  27. for (int i = 0; i < storage.Count; i++)
  28. {
  29. helparray = storage[i].Split('-');
  30. FillDic(inputammount);
  31. }
  32. FillOutPutDict();
  33. Print();
  34. }
  35.  
  36. private static void FillOutPutDict()
  37. {
  38. //грешни проверки. четете условиет
  39. if (dict.ContainsKey("Gold"))
  40. {
  41. output.Add("Gold", goldammountAll);
  42. }
  43. if (dict.ContainsKey("Cash"))
  44. {
  45. output.Add("Cash", cashammountAll);
  46. }
  47. if (dict.ContainsKey("Gem"))
  48. {
  49. output.Add("Gem", gemammountAll);
  50. }
  51. }
  52.  
  53. private static void Print()
  54. {
  55. foreach (var item in output.OrderByDescending(x => x.Value))
  56. {
  57. Console.WriteLine($"<{item.Key}> ${item.Value}");
  58. foreach (var item1 in dict[item.Key].OrderByDescending(x => x.Key).ThenBy(x => x.Value))
  59. {
  60. Console.WriteLine($"##{item1.Key} - {item1.Value}");
  61. }
  62. }
  63. }
  64.  
  65. private static void FillDic(long inputammount)
  66. {
  67. currtype = helparray[0];
  68. currammount = long.Parse(helparray[1]);
  69. long predict = goldammountAll + currammount + gemammountAll + cashammountAll;//еднакво за всички проверки
  70. if (isGold())
  71. {
  72. if (dict.ContainsKey("Gold") == false)
  73. {
  74. if (predict <= inputammount)
  75. {
  76. dict.Add("Gold", new Dictionary<string, long>());
  77. dict["Gold"].Add(currtype, currammount);
  78. goldammountAll += currammount;
  79. }
  80. }
  81. else
  82. {
  83. if (predict <= inputammount)
  84. {
  85. dict["Gold"][currtype] += currammount;
  86. goldammountAll += currammount;
  87. }
  88. }
  89. }
  90. if (isGem())
  91. {
  92. long currpredict = gemammountAll + currammount;
  93. if (dict.ContainsKey("Gem") == false)
  94. {
  95. if (predict <= inputammount && dict.ContainsKey("Gold") && currpredict <= goldammountAll)
  96. {
  97. dict.Add("Gem", new Dictionary<string, long>());
  98. dict["Gem"].Add(currtype, currammount);
  99. gemammountAll += currammount;
  100. }
  101. }
  102. else
  103. {
  104. if (predict <= inputammount && dict.ContainsKey("Gold") && currpredict <= goldammountAll)
  105. {
  106. if (dict["Gem"].ContainsKey(currtype) == false)
  107. {
  108. dict["Gem"].Add(currtype, currammount);
  109. gemammountAll += currammount;
  110. }
  111. else
  112. {
  113. dict["Gem"][currtype] += currammount;
  114. gemammountAll += currammount;
  115. }
  116. }
  117. }
  118. }
  119. if (isCash())
  120. {
  121. long currpredict = cashammountAll + currammount;
  122. if (dict.ContainsKey("Cash") == false)
  123. {
  124. if (predict <= inputammount && dict.ContainsKey("Gem") && currpredict <= gemammountAll)
  125. {
  126. dict.Add("Cash", new Dictionary<string, long>());
  127. dict["Cash"].Add(currtype, currammount);
  128. cashammountAll += currammount;
  129. }
  130. }
  131. else
  132. {
  133. if (predict <= inputammount && dict.ContainsKey("Gem") && currpredict <= gemammountAll)
  134. {
  135. if (dict["Cash"].ContainsKey(currtype) == false)
  136. {
  137. dict["Cash"].Add(currtype, currammount);
  138. cashammountAll += currammount;
  139. }
  140. else
  141. {
  142. dict["Cash"][currtype] += currammount;
  143. cashammountAll += currammount;
  144. }
  145. }
  146. }
  147. }
  148. }
  149.  
  150. private static bool isCash()
  151. {
  152. return currtype.Length == 3;//опростено
  153. }
  154.  
  155. private static bool isGem()
  156. {
  157. var copy = currtype.ToLower();
  158. return copy.EndsWith("gem") && currtype.Length >= 4;//опростено
  159. }
  160.  
  161. private static bool isGold()
  162. {
  163. return currtype == "Gold";//опростено
  164. }
  165.  
  166. private static void FillInput(string[] input)
  167. {
  168. storage = new List<string>();
  169. //string empty = string.Empty; ->ненуно
  170. for (int i = 0; i < input.Length; i += 2)
  171. {
  172. //empty = input[i] + "-" + input[i + 1]; -> ненужно
  173. storage.Add(input[i] + "-" + input[i + 1]);
  174. //empty = string.Empty; -> ненужно
  175. }
  176. }
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement