SophiYo

Problem02Judge

Mar 20th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. package O7AssocietiveArrays.MoreExercise;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.LinkedHashMap;
  7. import java.util.Map;
  8. import java.util.TreeMap;
  9.  
  10. public class Problem02Judge {
  11.     public static void main(String[] args) throws IOException {
  12.         BufferedReader reader =
  13.             new BufferedReader(
  14.                 new InputStreamReader(
  15.                     System.in
  16.                 )
  17.             );
  18.  
  19.         LinkedHashMap<String, TreeMap<String, Integer>> judgeStatistic = new LinkedHashMap<>();
  20.  
  21.         String input;
  22.         while (!"no more time".equalsIgnoreCase(input=reader.readLine())) {
  23.             String[] tokens = input.split("\\s+->\\s+");
  24.  
  25.             String user = tokens[0];
  26.             String contest = tokens[1];
  27.             int points = Integer.parseInt(tokens[2]);
  28.  
  29.             judgeStatistic.putIfAbsent(contest, new TreeMap<>());
  30.             if (judgeStatistic.get(contest).containsKey(user)) {
  31.                 if (judgeStatistic.get(contest).get(user) < points) {
  32.                     judgeStatistic.get(contest).put(user,points);
  33.                 }
  34.             } else {
  35.                 judgeStatistic.get(contest).put(user,points);
  36.             }
  37.  
  38.         }
  39.  
  40.         for (Map.Entry<String, TreeMap<String, Integer>> key : judgeStatistic
  41.                 .entrySet()) {
  42.             System.out.println(String.format("%s: %d participants",
  43.                     key.getKey(),
  44.                     key.getValue().size()));
  45.  
  46.             final int[] i = {0};
  47.             key
  48.                     .getValue()
  49.                     .entrySet()
  50.                     .stream()
  51.                     .sorted((f, t) -> {
  52.                         int sort = Integer.compare(t.getValue(), f.getValue());
  53.  
  54.                         if (sort == 0) {
  55.                             sort = f.getKey().compareTo(t.getKey());
  56.                         }
  57.                         return sort;
  58.  
  59.                     })
  60.                     .forEach((e) -> {
  61.  
  62.                         System.out.println(String.format("%d. %s <::> %d",
  63.                                 i[0] + 1,
  64.                                 e.getKey(),
  65.                                 e.getValue()));
  66.                         i[0]++;
  67.  
  68.  
  69.                     });
  70.         }
  71.  
  72.         System.out.println("Individual standings:");
  73.  
  74.         TreeMap<String, Integer> userPoints = new TreeMap<>();
  75.         for (Map.Entry<String, TreeMap<String, Integer>> kvp : judgeStatistic.entrySet()) {
  76.  
  77.             for (Map.Entry<String, Integer> sth : kvp.getValue().entrySet()) {
  78.                 userPoints.putIfAbsent(sth.getKey(), 0);
  79.  
  80.                 userPoints.put(sth.getKey(),
  81.                         userPoints.get(sth.getKey())
  82.                                 +sth.getValue());
  83.             }
  84.         }
  85.  
  86.         final int[] i = {0};
  87.         userPoints
  88.                 .entrySet()
  89.                 .stream()
  90.                 .sorted((e1,e2)->
  91.                         e2.getValue().compareTo(e1.getValue()))
  92.         .forEach(e-> {
  93.                     System.out.println(String.format("%d. %s -> %d",
  94.                             i[0]+1,
  95.                             e.getKey(),
  96.                             e.getValue()
  97.                             ));
  98.                     i[0]++;
  99.                 }
  100.                 );
  101.  
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment