|
1 | | -// /dev/stdin |
2 | | -const fs = require("fs"); |
| 1 | +/* /dev/stdin */ |
| 2 | +let fs = require("fs"); |
3 | 3 | let input = fs |
4 | 4 | .readFileSync("/dev/stdin") |
5 | 5 | .toString() |
6 | 6 | .split("\n") |
7 | | - .map((x) => x.replace("\r", "")); |
| 7 | + .map((x) => x.replaceAll("\r", "")); |
8 | 8 |
|
9 | 9 | const N = Number(input[0]); |
10 | | -const numberCards = input[1].split(" ").map(Number); |
11 | | -const M = Number(input[2]); |
12 | | -let myCards = input[3] |
| 10 | +const myCards = input[1] |
13 | 11 | .split(" ") |
14 | 12 | .map(Number) |
15 | 13 | .sort((a, b) => a - b); |
16 | 14 |
|
17 | | -const maps = new Map(); |
| 15 | +const M = Number(input[2]); |
| 16 | +const targetCards = input[3].split(" ").map(Number); |
| 17 | +const answer = []; |
| 18 | + |
| 19 | +for (const targetCard of targetCards) { |
| 20 | + const lower = lowerBound(myCards, targetCard); |
| 21 | + const upper = upperBound(myCards, targetCard); |
18 | 22 |
|
19 | | -for (const key of myCards) { |
20 | | - maps[key] = 0; |
| 23 | + const result = upper - lower; |
| 24 | + answer.push(result); |
21 | 25 | } |
| 26 | +console.log(answer.join(" ")); |
22 | 27 |
|
23 | | -let answer = ""; |
| 28 | +function lowerBound(myCards, targetCard) { |
| 29 | + let left = 0; |
| 30 | + let right = myCards.length; |
24 | 31 |
|
25 | | -for (const x of numberCards) { |
26 | | - const result = binarySearch(myCards, x); |
27 | | - if (result === 1) { |
28 | | - maps[x]++; |
| 32 | + while (left < right) { |
| 33 | + const mid = Math.floor((left + right) / 2); |
| 34 | + |
| 35 | + if (myCards[mid] >= targetCard) { |
| 36 | + right = mid; |
| 37 | + } else { |
| 38 | + left = mid + 1; |
| 39 | + } |
29 | 40 | } |
| 41 | + |
| 42 | + return left; |
30 | 43 | } |
31 | 44 |
|
32 | | -function binarySearch(cards, target) { |
| 45 | +function upperBound(myCards, targetCard) { |
33 | 46 | let left = 0; |
34 | | - let right = cards.length - 1; |
| 47 | + let right = myCards.length; |
35 | 48 |
|
36 | | - while (left <= right) { |
| 49 | + while (left < right) { |
37 | 50 | const mid = Math.floor((left + right) / 2); |
38 | 51 |
|
39 | | - if (cards[mid] === target) { |
40 | | - return 1; |
41 | | - } else if (cards[mid] < target) { |
42 | | - left = mid + 1; |
| 52 | + if (myCards[mid] > targetCard) { |
| 53 | + right = mid; |
43 | 54 | } else { |
44 | | - right = mid - 1; |
| 55 | + left = mid + 1; |
45 | 56 | } |
46 | 57 | } |
47 | 58 |
|
48 | | - return 0; |
49 | | -} |
50 | | -myCards = input[3].split(" ").map(Number); |
51 | | -for (const number of myCards) { |
52 | | - answer += maps[number] + " "; |
| 59 | + return left; |
53 | 60 | } |
54 | | - |
55 | | -console.log(answer); |
|
0 commit comments