Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Ranking {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, String> contestsAndPass = new LinkedHashMap<>();
- Map<String, Map<String, Integer>> userContestPoints = new TreeMap<>();
- String input = scanner.nextLine();
- while (!input.equals("end of contests")) {
- String[] contests = input.split(":");
- String contest = contests[0];
- String password = contests[1];
- contestsAndPass.put(contest, password);
- input = scanner.nextLine();
- }
- String submission = scanner.nextLine();
- while (!submission.equals("end of submissions")) {
- String[] subm = submission.split("=>");
- String contestName = subm[0];
- String passWord = subm[1];
- String username = subm[2];
- int points = Integer.parseInt(subm[3]);
- if (contestsAndPass.containsKey(contestName) && passWord.equals(contestsAndPass.get(contestName))) {
- if (!userContestPoints.containsKey(username)) {
- userContestPoints.put(username, new TreeMap<>());
- userContestPoints.get(username).put(contestName, points);
- } else {
- if (!userContestPoints.get(username).containsKey(contestName)) {
- userContestPoints.get(username).put(contestName, points);
- } else {
- int currPoints = userContestPoints.get(username).get(contestName);
- if (currPoints < points) {
- userContestPoints.get(username).put(contestName, points);
- }
- }
- }
- }
- submission = scanner.nextLine();
- }
- String bestStudent = "";
- int highestPoints = 0;
- int currPoints = 0;
- for (Map.Entry<String, Map<String, Integer>> entry : userContestPoints.entrySet()) {
- for (Map.Entry<String, Integer> entry2 : entry.getValue().entrySet()) {
- currPoints += entry2.getValue();
- }
- if (currPoints > highestPoints) {
- highestPoints = currPoints;
- currPoints = 0;
- bestStudent = entry.getKey();
- }
- }
- System.out.printf("Best candidate is %s with total %s points.%n", bestStudent, highestPoints);
- System.out.println("Ranking: ");
- List<Map.Entry<String, Map<String, Integer>>> listToSort = new ArrayList<>();
- for (Map.Entry<String, Map<String, Integer>> entry : userContestPoints.entrySet()) {
- listToSort.add(entry);
- }
- listToSort.sort(Comparator.comparing(Map.Entry::getKey));
- for (Map.Entry<String, Map<String, Integer>> entry : listToSort) {
- System.out.println(entry.getKey());
- List<Map.Entry<String, Integer>> toSort = new ArrayList<>();
- for (Map.Entry<String, Integer> a : entry.getValue().entrySet()) {
- toSort.add(a);
- }
- toSort.sort((a, b) -> b.getValue().compareTo(a.getValue()));
- for (Map.Entry<String, Integer> a : toSort) {
- System.out.printf("# %s -> %d\n", a.getKey(), a.getValue());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement