第一题:牛牛找工作
题目:
为了找到自己满意的工作,牛牛收集了每种工作的难度和报酬。牛牛选工作的标准是在难度不超过自身能力值的情况下,牛牛选择报酬最高的工作。在牛牛选定了自己的工作后,牛牛的小伙伴们来找牛牛帮忙选工作,牛牛依然使用自己的标准来帮助小伙伴们。牛牛的小伙伴太多了,于是他只好把这个任务交给了你。
输入:
每个输入包含一个测试用例。
每个测试用例的第一行包含两个正整数,分别表示工作的数量和小伙伴的数量。
接下来的N
行每行包含两个正整数,分别表示该项工作的难度和报酬。
接下来的一行包含M
个正整数,分别表示M
个小伙伴的能力值。
保证不存在两项工作的报酬相同。
输出:
对于每个小伙伴,在单独的一行输出一个正整数表示他能得到的最高报酬。一个工作可以被多个人选择。
样例:
in:
3 3
1 100
10 1000
1000000000 1001
9 10 1000000000
out:
100
1000
1001
解析:暴力搜索循环遍历每一个工作难度小于个人能力的工作,并在满足条件的工作中取最大值,则为该小伙伴所能得打的最高报仇。时间复杂度为O(N*N),不能满足时间要求。
/*import java.util.Scanner;
import java.util.TreeMap;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int[] hards = new int[M];
TreeMap<Integer, Integer> DP = new TreeMap<Integer, Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
return o2-o1;
}
});
for(int i = 0; i < N; i++) {
int d = sc.nextInt();
int p = sc.nextInt();
DP.put(d, p);
}
for(int i = 0; i < M; i++) {
hards[i] = sc.nextInt();
}
int count = 0;
for(int i = 0; i < M; i++) {
int max = 0;
for(int d :DP.keySet()) {
if(hards[i] >= d && max < DP.get(d)) {
max = DP.get(d);
}
}
System.out.println(max);
}
}
}*/
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]));
}
}
}