Advertisement
vlastomar

Untitled

Oct 31st, 2020
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1.  
  2. package greedyTimes;
  3.  
  4. import java.util.Collection;
  5. import java.util.LinkedHashMap;
  6. import java.util.Map;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10. public static void main(String[] args) {
  11.  
  12. Scanner scanner = new Scanner(System.in);
  13. long entrance = Long.parseLong(scanner.nextLine());
  14. String[] caseS = scanner.nextLine().split("\\s+");
  15.  
  16. var bag = new LinkedHashMap<String, LinkedHashMap<String, Long>>();
  17. long gold = 0;
  18. long stones = 0;
  19. long money = 0;
  20.  
  21. for (int i = 0; i < caseS.length; i += 2) {
  22. String name = caseS[i];
  23. long number = Long.parseLong(caseS[i + 1]);
  24.  
  25. String items = checkForItems(name);
  26.  
  27. if (items.equals("")) {
  28. continue;
  29. } else if (entrance < bag.values().stream().map(Map::values).flatMap(Collection::stream).mapToLong(e -> e).sum() + number) {
  30. continue;
  31. }
  32.  
  33. switch (items) {
  34. case "Gem":
  35. if (!bag.containsKey(items)) {
  36. if (bag.containsKey("Gold")) {
  37. if (number > bag.get("Gold").values().stream().mapToLong(e -> e).sum()) {
  38. continue;
  39. }
  40. } else {
  41. continue;
  42. }
  43. } else if (bag.get(items).values().stream().mapToLong(e -> e).sum() + number > bag.get("Gold").values().stream().mapToLong(e -> e).sum()) {
  44. continue;
  45. }
  46. break;
  47. case "Cash":
  48. if (!bag.containsKey(items)) {
  49. if (bag.containsKey("Gem")) {
  50. if (number > bag.get("Gold").values().stream().mapToLong(e -> e).sum()) {
  51. continue;
  52. }
  53. } else {
  54. continue;
  55. }
  56. } else if (bag.get(items).values().stream().mapToLong(e -> e).sum() + number > bag.get("Gem").values().stream().mapToLong(e -> e).sum()) {
  57. continue;
  58. }
  59. break;
  60. }
  61.  
  62. fillMapWIthKey(bag,items,name);
  63.  
  64. bag.get(items).put(name, bag.get(items).get(name) + number);
  65.  
  66. if (items.equals("Gold")) {
  67. gold += number;
  68. } else if (items.equals("Gem")) {
  69. stones += number;
  70. } else if (items.equals("Cash")) {
  71. money += number;
  72. }
  73. }
  74. printBad(bag);
  75.  
  76. }
  77.  
  78. private static void printBad(LinkedHashMap<String, LinkedHashMap<String, Long>> bag) {
  79. for (var x : bag.entrySet()) {
  80. Long sumValues = x.getValue().values().stream().mapToLong(l -> l).sum();
  81.  
  82. System.out.println(String.format("<%s> $%s", x.getKey(), sumValues));
  83.  
  84. x.getValue().entrySet().stream().sorted((e1, e2) -> e2.getKey().compareTo(e1.getKey())).forEach(i -> System.out.println("##" + i.getKey() + " - " + i.getValue()));
  85.  
  86. }
  87. }
  88.  
  89. private static void fillMapWIthKey(LinkedHashMap<String, LinkedHashMap<String, Long>> bag, String items, String name) {
  90. if (!bag.containsKey(items)) {
  91. bag.put((items), new LinkedHashMap<String, Long>());
  92. }
  93.  
  94. if (!bag.get(items).containsKey(name)) {
  95. bag.get(items).put(name, 0L);
  96. }
  97. }
  98.  
  99. private static String checkForItems(String name) {
  100. String items = "";
  101. if (name.length() == 3) {
  102. items = "Cash";
  103. } else if (name.toLowerCase().endsWith("gem")) {
  104. items = "Gem";
  105. } else if (name.toLowerCase().equals("gold")) {
  106. items = "Gold";
  107. }
  108. return items;
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement