題目描述
爲了找到自己滿意的工作,牛牛收集了每種工作的難度和報酬。牛牛選工作的標準是在難度不超過自身能力值的情況下,牛牛選擇報酬最高的工作。在牛牛選定了自己的工作後,牛牛的小夥伴們來找牛牛幫忙選工作,牛牛依然使用自己的標準來幫助小夥伴們。牛牛的小夥伴太多了,於是他只好把這個任務交給了你。
輸入描述:
每個輸入包含一個測試用例。 每個測試用例的第一行包含兩個正整數,分別表示工作的數量N(N<=100000)和小夥伴的數量M(M<=100000)。 接下來的N行每行包含兩個正整數,分別表示該項工作的難度Di(Di<=1000000000)和報酬Pi(Pi<=1000000000)。 接下來的一行包含M個正整數,分別表示M個小夥伴的能力值Ai(Ai<=1000000000)。 保證不存在兩項工作的報酬相同。
輸出描述:
對於每個小夥伴,在單獨的一行輸出一個正整數表示他能得到的最高報酬。一個工作可以被多個人選擇。
示例1
輸入
3 3 1 100 10 1000 1000000000 1001 9 10 1000000000
輸出
100 1000 1001
代碼:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int t1=0,t2=0;
HashMap<Integer,Integer> hs = new HashMap<>();
int[] a = new int[n+m];
int[] b = new int[m];
for(int i=0;i<n;i++){
t1 = sc.nextInt();
t2 = sc.nextInt();
a[i] = t1;
hs.put(t1,t2);
}
for(int i=0;i<m;i++){
t1 = sc.nextInt();
a[n+i] = t1;
b[i] = t1;
if(!hs.containsKey(t1))
hs.put(t1,0);
}
Arrays.sort(a);
int max = 0;
for(int i=0;i<m+n;i++){
max = Math.max(max,hs.get(a[i]));
hs.put(a[i],max);
}
for(int i=0;i<m;i++){
System.out.println(hs.get(b[i]));
}
}
}