Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.TreeMap;
- public class P4_CubicAssault {
- public static void main(String[] args) throws IOException {
- BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
- TreeMap<String, TreeMap<String, Long>> map = new TreeMap<>();
- while (true) {
- String input = console.readLine();
- if (input.equals("Count em all")) {
- break;
- }
- String[] line = input.split(" -> ");
- String regionName = line[0];
- String meteorType = line[1];
- int count = Integer.parseInt(line[2]);
- if (!map.containsKey(regionName)) {
- TreeMap<String, Long> meteorCount = new TreeMap<>();
- meteorCount.put("Red", 0L);
- meteorCount.put("Black", 0L);
- meteorCount.put("Green", 0L);
- map.put(regionName, meteorCount);
- }
- long oldValue = map.get(regionName).get(meteorType);
- map.get(regionName).put(meteorType, oldValue + count);
- }
- for (String region : map.keySet()) {
- TreeMap<String, Long> newMap = map.get(region);
- long greenValue = newMap.get("Green");
- long redValue = newMap.get("Red");
- long blackValue = newMap.get("Black");
- long temp = greenValue / 1000000;
- greenValue = greenValue % 1000000;
- redValue = redValue + temp;
- temp = redValue / 1000000;
- redValue = redValue % 1000000;
- blackValue = blackValue + temp;
- newMap.put("Green", greenValue);
- newMap.put("Red", redValue);
- newMap.put("Black", blackValue);
- map.put(region, newMap);
- }
- map.entrySet().stream().sorted((s1, s2) -> {
- long f1 = s1.getValue().get("Black");
- long f2 = s2.getValue().get("Black");
- if (f1 != f2) {
- return Long.compare(f2, f1);
- } else {
- int f1Length = s1.getKey().length();
- int f2Length = s2.getKey().length();
- return Integer.compare(f1Length, f2Length);
- }
- }).forEach(region -> {
- System.out.println(region.getKey());
- TreeMap<String, Long> newMap = map.get(region.getKey());
- newMap.entrySet().stream()
- .sorted((m1, m2) -> m2.getValue().compareTo(m1.getValue()))
- .forEach(meteor -> System.out.printf("-> %s : %d%n"
- , meteor.getKey()
- , meteor.getValue()));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement