Advertisement
Guest User

Untitled

a guest
Jun 20th, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.TreeMap;
  5.  
  6. public class P4_CubicAssault {
  7.  
  8. public static void main(String[] args) throws IOException {
  9.  
  10. BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
  11. TreeMap<String, TreeMap<String, Long>> map = new TreeMap<>();
  12.  
  13. while (true) {
  14. String input = console.readLine();
  15. if (input.equals("Count em all")) {
  16. break;
  17. }
  18.  
  19. String[] line = input.split(" -> ");
  20. String regionName = line[0];
  21. String meteorType = line[1];
  22. int count = Integer.parseInt(line[2]);
  23.  
  24. if (!map.containsKey(regionName)) {
  25. TreeMap<String, Long> meteorCount = new TreeMap<>();
  26. meteorCount.put("Red", 0L);
  27. meteorCount.put("Black", 0L);
  28. meteorCount.put("Green", 0L);
  29. map.put(regionName, meteorCount);
  30. }
  31. long oldValue = map.get(regionName).get(meteorType);
  32. map.get(regionName).put(meteorType, oldValue + count);
  33. }
  34.  
  35. for (String region : map.keySet()) {
  36. TreeMap<String, Long> newMap = map.get(region);
  37.  
  38. long greenValue = newMap.get("Green");
  39. long redValue = newMap.get("Red");
  40. long blackValue = newMap.get("Black");
  41.  
  42. long temp = greenValue / 1000000;
  43. greenValue = greenValue % 1000000;
  44. redValue = redValue + temp;
  45. temp = redValue / 1000000;
  46. redValue = redValue % 1000000;
  47. blackValue = blackValue + temp;
  48.  
  49. newMap.put("Green", greenValue);
  50. newMap.put("Red", redValue);
  51. newMap.put("Black", blackValue);
  52. map.put(region, newMap);
  53. }
  54.  
  55. map.entrySet().stream().sorted((s1, s2) -> {
  56. long f1 = s1.getValue().get("Black");
  57. long f2 = s2.getValue().get("Black");
  58.  
  59. if (f1 != f2) {
  60. return Long.compare(f2, f1);
  61. } else {
  62. int f1Length = s1.getKey().length();
  63. int f2Length = s2.getKey().length();
  64. return Integer.compare(f1Length, f2Length);
  65. }
  66. }).forEach(region -> {
  67. System.out.println(region.getKey());
  68. TreeMap<String, Long> newMap = map.get(region.getKey());
  69.  
  70. newMap.entrySet().stream()
  71. .sorted((m1, m2) -> m2.getValue().compareTo(m1.getValue()))
  72. .forEach(meteor -> System.out.printf("-> %s : %d%n"
  73. , meteor.getKey()
  74. , meteor.getValue()));
  75. });
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement