Java实现,参考了回答中“https://www.nowcoder.com/questionTerminal/46e837a4ea9144f5ad2021658cb54c4d?f=discussion”山柴帆的解题思路。
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int M = scan.nextInt();
// arrN 保存不同报酬所需的能力值
int[] arrN = new int[N+M];
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<N; i++) {
int tempK = scan.nextInt();
int tempV = scan.nextInt();
arrN[i] = tempK;
if(map.containsKey(tempK)) {
int comp = map.get(tempK);
tempV = comp>tempV ? comp : tempV;
map.put(tempK, tempV);
}else {
map.put(tempK, tempV);
}
}
// arrM 保存M个小伙伴的能力值
int[] arrM = new int[M];
for(int i=0; i<M; i++) {
arrM[i] = scan.nextInt();
// 将小伙伴的能力值更新到map中
if(!map.containsKey(arrM[i])) {
map.put(arrM[i], 0);
}
arrN[N+i] = arrM[i];
}
scan.close();
// 排序arrN
Arrays.sort(arrN);
// 将map中的各个key的value更新为其可能的最大value值(此步骤会将小伙伴的能力值所能够获得的最大value进行更新)
int curMax = map.get(arrN[0]);
Set<Integer> keyArr = map.keySet();
Integer[] arr = new Integer[map.size()];
arr = keyArr.toArray(arr);
Arrays.sort(arr);
for(int i=0; i<arr.length; i++) {
curMax = Math.max(curMax, map.get(arr[i]));
if(curMax>map.get(arr[i])) {
map.put(arr[i], curMax);
}
}
//
for(int i=0; i<M; i++) {
System.out.println(map.get(arrM[i]));
}
}
}

京公网安备 11010502036488号