-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueriesLessEqualElement.java
42 lines (41 loc) · 1.18 KB
/
QueriesLessEqualElement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* https://codeforces.com/problemset/problem/600/B #sorting #binary-search #two-pointer sort the
* first array, use binarySearch each element of second array.
*/
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
public class QueriesLessEqualElement {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
Integer[] firstArr = new Integer[n];
int[] result = new int[m];
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0; i < n; i++) {
int tmp = sc.nextInt();
firstArr[i] = tmp;
}
Arrays.sort(firstArr, Collections.reverseOrder());
for (int i = 0; i < n; i++) {
int tmp = firstArr[i];
if (!hashMap.containsKey(tmp)) {
hashMap.put(tmp, i);
}
}
for (int i = 0; i < m; i++) {
int tmp = sc.nextInt();
int r = Arrays.binarySearch(firstArr, tmp, Collections.reverseOrder());
if (r < 0) {
result[i] = n + r + 1;
} else {
result[i] = n - hashMap.get(tmp);
}
}
for (int r : result) {
System.out.print(r + " ");
}
}
}