Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AssociativeArraysExercise;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.LinkedHashMap;
- import java.util.Map;
- public class Judge {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String line = reader.readLine();
- Map<String, Map<String, Integer>> studetnByContest = new LinkedHashMap<>();
- while (!line.equals("no more time")){
- String[] tokens = line.split(" -> ");
- String username = tokens[0];
- String contest = tokens[1];
- int points = Integer.parseInt(tokens[2]);
- if (!studetnByContest.containsKey(contest)){
- studetnByContest.put(contest,new LinkedHashMap<>());
- studetnByContest.get(contest).put(username,points);
- } else {
- if (studetnByContest.get(contest).containsKey(username)){
- int highestScore = 0;
- if (studetnByContest.get(contest).get(username) > highestScore){
- highestScore = points;
- studetnByContest.get(contest).put(username,points);
- }
- } else {
- studetnByContest.get(contest).put(username,points);
- }
- }
- line = reader.readLine();
- }
- Map<String, Integer> pointsByParticipant = new LinkedHashMap<>();
- for (Map.Entry<String,Map<String,Integer>> entry : studetnByContest.entrySet()){
- for (Map.Entry<String,Integer> points : entry.getValue().entrySet()){
- String participant = points.getKey();
- int totalPoints = points.getValue();
- if (!pointsByParticipant.containsKey(participant)){
- pointsByParticipant.put(participant,totalPoints);
- } else {
- pointsByParticipant.put(participant,totalPoints + pointsByParticipant.get(participant));
- }
- }
- }
- int[] num = new int[1];
- studetnByContest.entrySet().stream().forEach(e -> {
- num[0] = 0;
- System.out.println(e.getKey() + ": " + e.getValue().size() + " participants");
- e.getValue().entrySet().stream().sorted((a,b) -> {b.getValue().compareTo(a.getValue());
- int result = Integer.compare(b.getValue(),a.getValue());
- if (result == 0){
- result = a.getKey().compareTo(b.getKey());
- }
- return result;
- }).forEach(b -> System.out.printf("%d. %s <::> %d%n", ++num[0],b.getKey(),b.getValue()));
- });
- num[0] = 0;
- System.out.println("Individual standings:");
- pointsByParticipant.entrySet().stream().sorted((a,b) -> {b.getValue().compareTo(a.getValue());
- int result = Integer.compare(b.getValue(),a.getValue());
- if (result == 0){
- result = a.getKey().compareTo(b.getKey());
- }
- return result;
- }).forEach(b -> System.out.printf("%d. %s -> %d%n",++num[0],b.getKey(),b.getValue()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement