import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
TreeMap<Integer, Long> map = new TreeMap<>();
for(int i = 0; i < n; i++){
int a = in.nextInt();
long b = in.nextLong();
if(map.containsKey(a)){
long c = map.get(a)+b;
map.put(a, c);
}else{
map.put(a, b);
}
}
for(int a: map.keySet()){
System.out.println(a + " " + map.get(a));
}
}
}
无法使用数组哈希的话,xi的范围超过了;hashmap的key是根据键(key)的哈希值来确定存储位置。哈希值是通过调用键(key)对象的 hashCode() 方法计算得到的,这个哈希值决定了键值对在哈希表中的存储位置。因此,HashMap 中的键(key)的存储顺序与它们的大小无关,而是与它们的哈希值有关。所以本题使用Treemap,TreeMap 是一个有序的映射,它可以根据键的自然顺序或自定义的比较器进行排序。
#牛客春招刷题训练营# https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201



京公网安备 11010502036488号