Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.util.SortedMap;
- import java.util.TreeMap;
- public class ActivityTracker {
- public static void main(String[] args) {
- Comparator<Entry<String, Integer>> cValue = new Comparator<Entry<String, Integer>>() {
- @Override
- public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
- return o2.getValue().compareTo(o1.getValue());
- }
- };
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- SortedMap<Integer, TreeMap<String, Integer>> activity = new TreeMap<>();
- for (int i = 0; i < n; i++) {
- String date = sc.next("[0-9]{2}/[0-9]{2}/[0-9]{4}");
- int month = Integer.parseInt(date.substring(3, 5));
- String name = sc.next();
- int distance = sc.nextInt();
- if (!(activity.containsKey(month))) {
- activity.put(month, new TreeMap<String, Integer>());
- }
- TreeMap<String, Integer> dist = activity.get(month);
- int d = 0;
- if (dist.containsKey(name)) {
- d = dist.get(name);
- }
- dist.put(name, d + distance);
- }
- sc.close();
- for (Integer month : activity.keySet()) {
- List<Map.Entry<String, Integer>> l = new ArrayList<Map.Entry<String, Integer>>(activity.get(month).entrySet());
- Collections.sort(l, cValue);
- System.out.print(month + ": ");
- boolean first = true;
- for (Entry<String, Integer> entry : l) {
- if (!first) {
- System.out.print(", ");
- }
- first = false;
- System.out.print(entry.getKey() + "(" + entry.getValue() + ")");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement