forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
83 lines (65 loc) · 1.63 KB
/
Main.java
File metadata and controls
83 lines (65 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// n m
// 1 1
// 2 2
// 3 3
// 4 4
// 4 4
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // n
int m = sc.nextInt(); // m
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
int id = sc.nextInt();
int score = sc.nextInt();
if (map.containsKey(id)) {
map.put(id, map.get(id) + score);
} else {
map.put(id, score);
}
}
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
// 按照value排序
Collections.sort(list, (o1, o2) -> {
return o2.getValue() - o1.getValue();
});
for (int i = 0; i < m; i++) {
System.out.println(list.get(i).getKey());
}
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x){val = x;}
}
class ListNode {
int val;
ListNode next;
public ListNode(int val) {
this.val = val;
}
}
class ListNodeTools {
public static ListNode getListNode(int[] nums) {
ListNode dummy = new ListNode(-1);
ListNode pre = dummy;
for (int num : nums) {
pre.next = new ListNode(num);
pre = pre.next;
}
return dummy.next;
}
public static void printListNode(ListNode node) {
String s = "";
ListNode p = node;
while (p != null) {
s += p.val + "->";
p = p.next;
}
System.out.println(s+"null");
}
}