Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.TreeMap;
- public class P04_RankingProblemDescription {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- Map<String, String> contests = new HashMap<>();
- while (true) {
- String input = reader.readLine();
- if ("end of contests".equals(input)) {
- break;
- }
- String[] tokens = input.split(":");
- String contest = tokens[0];
- String pass = tokens[1];
- contests.putIfAbsent(contest, pass);
- }
- Map<String, Map<String, Integer>> people = new TreeMap<>();
- while (true) {
- String input = reader.readLine();
- if ("end of submissions".equals(input)) {
- break;
- }
- String[] tokens = input.split("=>");
- String contest = tokens[0];
- String pass = tokens[1];
- String candidate = tokens[2];
- Integer points = Integer.parseInt(tokens[3]);
- if (contests.containsKey(contest) && contests.get(contest).equals(pass)) {
- if (!people.containsKey(candidate)) {
- people.put(candidate, new LinkedHashMap<>());
- people.get(candidate).put(contest, points);
- } else {
- if (!people.get(candidate).containsKey(contest)) {
- people.get(candidate).put(contest, points);
- } else {
- if (points > people.get(candidate).get(contest)) {
- people.get(candidate).put(contest, points);
- }
- }
- }
- }
- }
- String bestPerson = "";
- int bestScore = Integer.MIN_VALUE;
- for (Map.Entry<String, Map<String, Integer>> person : people.entrySet()) {
- String currentPerson = person.getKey();
- int personScore = 0;
- for (Map.Entry<String, Integer> personStats : person.getValue().entrySet()) {
- personScore += personStats.getValue();
- }
- if (personScore > bestScore) {
- bestScore = personScore;
- bestPerson = currentPerson;
- }
- }
- System.out.println(String.format("Best candidate is %s with total %d points.", bestPerson, bestScore));
- System.out.println("Ranking:");
- people.forEach((key, value) -> {
- System.out.println(key);
- value.entrySet()
- .stream()
- .sorted((first, second) -> -first.getValue().compareTo(second.getValue()))
- .forEach(contest -> System.out.printf("# %s -> %d%n", contest.getKey().trim(), contest.getValue()));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement