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.*;
- import java.util.stream.Collectors;
- public class p04_Ranking {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- Map<String, String> contestMap = new HashMap<>();
- Map<String, Map<String, Long>> map = new TreeMap<>();
- String input = reader.readLine();
- while (!"end of contests".equals(input)) {
- String[] splitInput = input.split(":");
- String contest = splitInput[0];
- String password = splitInput[1];
- if (!contestMap.containsKey(contest)) {
- contestMap.put(contest, password);
- }
- input = reader.readLine();
- }
- //C# Fundamentals=>fundPass=>Tanya=>350
- String line = reader.readLine();
- while (!"end of submissions".equals(line)) {
- String[] splitLine = line.split("=>");
- String course = splitLine[0];
- String pass = splitLine[1];
- String name = splitLine[2];
- Long points = Long.parseLong(splitLine[3]);
- if (!(contestMap.containsKey(course))) {
- line = reader.readLine();
- continue;
- }
- if (!contestMap.get(course).equals(pass)) {
- line = reader.readLine();
- continue;
- }
- if (!map.containsKey(name)) {
- map.put(name, new HashMap<>());
- }
- if (!map.get(name).containsKey(course)) {
- map.get(name).put(course, 0L);
- }
- if (points > map.get(name).get(course)) {
- map.get(name).put(course, points);
- }
- line = reader.readLine();
- }
- Map<String, Long> totals = map.entrySet()
- .stream()
- .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), e.getValue().entrySet().stream().mapToLong(Map.Entry::getValue).sum()))
- .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
- //System.out.println(totals);
- String nameWithBestSum ="";
- long n1 =0L;
- for (String s : totals.keySet()) {
- nameWithBestSum = s;
- // System.out.println("This is what you need "+ nameWithBestSum);
- for (Long aLong : totals.values()) {
- n1 = aLong;
- //System.out.println("This is " + n1);
- break;
- }
- break;
- }
- System.out.printf("Best candidate is %s with total %d points.%n", nameWithBestSum, n1);
- System.out.println("Ranking: ");
- map.entrySet().stream()
- .forEach(s -> {
- System.out.println(s.getKey());
- map.entrySet().stream().sorted((k1, k2) -> Long.compare(k2.getValue().values().stream().mapToLong(Long::longValue).sum(),
- k1.getValue().values().stream().mapToLong(Long::longValue).sum())) ;
- s.getValue().entrySet().stream()
- .sorted((k1, k2) -> Long.compare(k2.getValue(),
- k1.getValue()))
- .forEach(a -> System.out.println("# " + a.getKey() + " -> " + a.getValue()));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement