Advertisement
Edzhevit

Judge

Nov 22nd, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package AssociativeArraysExercise;
  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.  
  9. public class Judge {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.         String line = reader.readLine();
  14.  
  15.         Map<String, Map<String, Integer>> studetnByContest = new LinkedHashMap<>();
  16.  
  17.         while (!line.equals("no more time")){
  18.             String[] tokens = line.split(" -> ");
  19.             String username = tokens[0];
  20.             String contest = tokens[1];
  21.             int points = Integer.parseInt(tokens[2]);
  22.  
  23.             if (!studetnByContest.containsKey(contest)){
  24.                 studetnByContest.put(contest,new LinkedHashMap<>());
  25.                 studetnByContest.get(contest).put(username,points);
  26.             } else {
  27.                 if (studetnByContest.get(contest).containsKey(username)){
  28.                     int highestScore = 0;
  29.                     if (studetnByContest.get(contest).get(username) > highestScore){
  30.                         highestScore = points;
  31.                         studetnByContest.get(contest).put(username,points);
  32.                     }
  33.                 } else {
  34.                     studetnByContest.get(contest).put(username,points);
  35.                 }
  36.  
  37.             }
  38.  
  39.             line = reader.readLine();
  40.         }
  41.  
  42.         Map<String, Integer> pointsByParticipant = new LinkedHashMap<>();
  43.  
  44.  
  45.         for (Map.Entry<String,Map<String,Integer>> entry : studetnByContest.entrySet()){
  46.             for (Map.Entry<String,Integer> points : entry.getValue().entrySet()){
  47.                 String participant = points.getKey();
  48.                 int totalPoints = points.getValue();
  49.  
  50.                 if (!pointsByParticipant.containsKey(participant)){
  51.                     pointsByParticipant.put(participant,totalPoints);
  52.                 } else {
  53.                     pointsByParticipant.put(participant,totalPoints + pointsByParticipant.get(participant));
  54.                 }
  55.             }
  56.         }
  57.  
  58.         int[] num = new int[1];
  59.         studetnByContest.entrySet().stream().forEach(e -> {
  60.             num[0] = 0;
  61.             System.out.println(e.getKey() + ": " + e.getValue().size() +  " participants");
  62.             e.getValue().entrySet().stream().sorted((a,b) -> {b.getValue().compareTo(a.getValue());
  63.                 int result = Integer.compare(b.getValue(),a.getValue());
  64.  
  65.                 if (result == 0){
  66.                     result = a.getKey().compareTo(b.getKey());
  67.                 }
  68.                 return result;
  69.             }).forEach(b -> System.out.printf("%d. %s <::> %d%n", ++num[0],b.getKey(),b.getValue()));
  70.         });
  71.         num[0] = 0;
  72.         System.out.println("Individual standings:");
  73.         pointsByParticipant.entrySet().stream().sorted((a,b) -> {b.getValue().compareTo(a.getValue());
  74.  
  75.             int result = Integer.compare(b.getValue(),a.getValue());
  76.  
  77.             if (result == 0){
  78.                 result = a.getKey().compareTo(b.getKey());
  79.             }
  80.             return result;
  81.         }).forEach(b -> System.out.printf("%d. %s -> %d%n",++num[0],b.getKey(),b.getValue()));
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement