Netease 2019 written test Niu Niu find a job Java solution

Title description

In order to find a satisfactory job, Niu Niu collected the difficulty and reward of each job. The criterion for choosing a job is to choose the job with the highest remuneration if the difficulty does not exceed your own ability value. After Niu Niu chose his job, Niu Niu's friends came to Niu Niu to help him choose a job. Niu Niu still used his own standards to help his friends. There are too many Niu Niu's friends, so he has to give this task to you.

Enter description

Each input contains a test case. 
The first line of each test case contains two positive integers, representing the number of jobs N (N <= 100000) and the number of friends M (M <= 100000). 
Each of the next N lines contains two positive integers, indicating the difficulty Di (Di <= 1000000000) and reward Pi (Pi <= 1000000000) of the job. 
The next line contains M positive integers, which represent the ability value Ai of M small partners (Ai <= 1000000000). 
Ensure that no two jobs have the same remuneration.

Output description

For each partner, output a positive integer on a separate line to indicate the highest reward he can get. A job can be selected by multiple people.

Input example

3 3 
1 100 
10 1000 
1000000000 1001 
9 10 1000000000

Sample output

100 
1000 
1001

analysis

This question looks like the 01 backpack problem in dynamic planning, but it is not. At first, I thought it was a dynamic planning problem. I wrote a 01 backpack directly and found that java memory overflowed. Words: The standard for choosing a job is to choose the job with the highest remuneration when the difficulty does not exceed your own ability value. That is to say, in the work whose job difficulty is less than or equal to its own ability, choose the highest salary among them, so it is not dynamic rules, but sorting and searching.

Solution: I made a detailed comment in the code, and I know at a glance that I have run it on Niuke.com and can directly copy

import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeMap;

public class 牛牛找工作 {
    public static void main(String[] args) {
        //输入流
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();//n表示工作的数量
        int m = sc.nextInt();//m表示同学的数量

        // jobs数组 n行2列 n行表示n个工作,第一列表示工作的难度, 第二列表示工作的报酬
        int[][] jobs = new int[n][2];
        for (int i=0;i<n;i++){
            jobs[i][0] = sc.nextInt();//表示工作的难度
            jobs[i][1] = sc.nextInt();//表示工作的报酬
        }

        //jobs数组排序
        Arrays.sort(jobs, ((o1, o2) -> o1[0]-o2[0]));//按照工作难度从小到大排序

        //很重要 我们把每个工作的报酬设置为 难度比他小的这些工作中的最大报酬
        for (int i=1;i<jobs.length; i++){
            jobs[i][1] = Math.max(jobs[i - 1][1], jobs[i][1]);
        }

        //将job数组存储到TreeMap中,其中,key=难度,value=最大报酬
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int i=0; i<jobs.length; i++){
            map.put(jobs[i][0], jobs[i][1]);
        }

        //输入每个同学的能力ability
        for (int i=0;i<m;i++){
            int ability = sc.nextInt();
            //返回的是小于等于ability的最大key,其实这里就是在找这个能力下的最大工作难度
            Integer key = map.floorKey(ability);
            if (key != null)
                //找到这个难度对应的最大报酬就是我们想得到的最大报酬
                System.out.println(map.get(key));
            else
                System.out.println(0);
        }



    }
}

 

Published 111 original articles · Like 60 · 70,000 + views

Guess you like

Origin blog.csdn.net/Haidaiya/article/details/105475404