Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package.BasicExercise.MapsLambdaStream;
- import java.util.*;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- public class Ranking {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- Map<String, String> contestsPasswords = new HashMap<>();
- Map<String, Map<String, Integer>> usersContestsPoints = new TreeMap<>();
- String line = scan.nextLine();
- while (!line.equals("end of contests")) {
- String[] tokens = line.split(":");
- String contestName = tokens[0];
- String contestPassword = tokens[1];
- contestsPasswords.put(contestName, contestPassword);
- line = scan.nextLine();
- }
- line = scan.nextLine();
- while (!line.equals("end of submissions")) {
- String[] tokens = line.split("=>");
- String contestName = tokens[0];
- String contestPassword = tokens[1];
- String userName = tokens[2];
- int points = Integer.parseInt(tokens[3]);
- if (contestsPasswords.containsKey(contestName) &&
- contestsPasswords.get(contestName).equals(contestPassword)) {
- if (usersContestsPoints.get(userName) == null) {
- Map<String, Integer> tempUsersPoints = new HashMap<>();
- tempUsersPoints.put(contestName, points);
- usersContestsPoints.put(userName, tempUsersPoints);
- } else {
- if (!usersContestsPoints.get(userName).containsKey(contestName)) {
- usersContestsPoints.get(userName).put(contestName, points);
- } else if (usersContestsPoints.get(userName).get(contestName) < points) {
- usersContestsPoints.get(userName).put(contestName, points);
- }
- }
- }
- line = scan.nextLine();
- }
- Map<String, Integer> bestUserMaxPoints = new HashMap<>();
- for (Map.Entry<String, Map<String, Integer>> entry : usersContestsPoints.entrySet()) {
- for (Map.Entry<String, Integer> userPoints : entry.getValue().entrySet()) {
- String name = entry.getKey();
- int points = userPoints.getValue();
- if (!bestUserMaxPoints.containsKey(name)) {
- bestUserMaxPoints.put(name, points);
- } else {
- bestUserMaxPoints.put(name, points + bestUserMaxPoints.get(name));
- }
- }
- }
- String name = bestUserMaxPoints.entrySet().
- stream().sorted((a, b) -> b.getValue().compareTo(a.getValue()))
- .findFirst().get().getKey();
- int points = bestUserMaxPoints.entrySet().stream().sorted((a, b) -> b.getValue().compareTo(a.getValue())).
- findFirst().get().getValue();
- System.out.println(String.format(("Best candidate is %s with total %d points."), name, points));
- System.out.println("Ranking: ");
- usersContestsPoints.entrySet().stream().forEach(e->{
- System.out.println(e.getKey());
- e.getValue().entrySet().stream().sorted((a, b) -> b.getValue().compareTo(a.getValue())).forEach(b -> System.out.println(String.format(("# %s -> %d"), b.getKey(), b.getValue())));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement