牛牛找工作(牛客真题)

题目描述:
链接:https://www.nowcoder.com/questionTerminal/46e837a4ea9144f5ad2021658cb54c4d
来源:牛客网

为了找到自己满意的工作,牛牛收集了每种工作的难度和报酬。牛牛选工作的标准是在难度不超过自身能力值的情况下,牛牛选择报酬最高的工作。在牛牛选定了自己的工作后,牛牛的小伙伴们来找牛牛帮忙选工作,牛牛依然使用自己的标准来帮助小伙伴们。牛牛的小伙伴太多了,于是他只好把这个任务交给了你。

输入描述:

每个输入包含一个测试用例。
每个测试用例的第一行包含两个正整数,分别表示工作的数量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[][] nums = new int[n][2];
        for (int i = 0; i < n; i++) {
            nums[i][0] = sc.nextInt();
            nums[i][1] = sc.nextInt();
        }
        Arrays.sort(nums,(o1,o2)->{
            return o1[0]-o2[0];
        });
        TreeMap<Integer,Integer> map = new TreeMap<>();
        map.put(nums[0][0],nums[0][1]);
        for (int i = 1; i < nums.length; i++) {
            nums[i][1] = Math.max(nums[i][1],nums[i-1][1]);
            map.put(nums[i][0],nums[i][1]);
        }
        for (int i = 0; i < m; i++) {
            int cur = sc.nextInt();
            Integer integer = map.floorKey(cur);
            if(integer != null){
                System.out.println(map.get(integer));
            }else {
                System.out.println(0);
            }
        }
    }
}
发布了258 篇原创文章 · 获赞 5 · 访问量 5328

猜你喜欢

转载自blog.csdn.net/weixin_43105156/article/details/104539481