Guest User

Untitled

a guest
Apr 10th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.atomic.AtomicInteger;
  3.  
  4.  
  5. public class AnonymousCache {
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         Map<String, Map<String, Integer>> contest = new HashMap<>();
  11.         Map<String, String> registeredContestants = new HashMap<>();
  12.         String data;
  13.  
  14.         while (!"END".equals(data = scanner.nextLine())) {
  15.  
  16.             String[] info = data.split("\\s+->\\s+");
  17.  
  18.             String contestant = info[1];
  19.             String country = info[0];
  20.  
  21.             registeredContestants.putIfAbsent(contestant, country);
  22.  
  23.             if (!registeredContestants.get(contestant).equals(country)) {
  24.                 continue;
  25.             }
  26.             int points = Integer.parseInt(info[2]);
  27.  
  28.             contest.putIfAbsent(country, new LinkedHashMap<>());
  29.             contest.get(country).putIfAbsent(contestant, 0);
  30.             contest.get(country).put(contestant, contest.get(country).get(contestant) + points);
  31.         }
  32.         AtomicInteger sum1 = new AtomicInteger();
  33.         AtomicInteger sum2 = new AtomicInteger();
  34.  
  35.         contest.entrySet().stream().sorted((pair1, pair2) -> {
  36.  
  37.             sum1.set(0);
  38.             sum2.set(0);
  39.  
  40.             pair1.getValue().forEach((key, value) -> sum1.addAndGet(value));
  41.             pair2.getValue().forEach((key, value) -> sum2.addAndGet(value));
  42.  
  43.             return Integer.compare(sum2.get(), sum1.get());
  44.         }).forEach(pair -> {
  45.  
  46.             sum1.set(0);
  47.  
  48.             pair.getValue().forEach((key, value) -> sum1.addAndGet(value));
  49.             System.out.println(String.format("%s: %d", pair.getKey(), sum1.get()));
  50.  
  51.             pair.getValue().forEach((key, value) -> System.out.println(String.format("-- %s -> %d"
  52.                     , key, value)));
  53.         });
  54.     }
  55. }
Add Comment
Please, Sign In to add comment