ALGORITHM/BOJ

[백준] 10816 숫자 카드 2

0298 2021. 9. 29. 23:30

https://www.acmicpc.net/problem/10816

 

10816번: 숫자 카드 2

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,

www.acmicpc.net

2021-09-27


1) HashMap

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
 
public class Main10816 {
    public static int N, M;
    public static HashMap<Integer, Integer> map;
    public static StringBuilder sb;
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine().trim());
        sb = new StringBuilder();
        N = Integer.parseInt(st.nextToken());
        map = new HashMap<>();
        st = new StringTokenizer(bf.readLine());
        for(int i = 0; i < N; i++) {
            int num = Integer.parseInt(st.nextToken());
            map.put(num, map.getOrDefault(num, 0+ 1);
        }
 
        st = new StringTokenizer(bf.readLine().trim());
        M = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(bf.readLine());
        for(int i = 0; i < M; i++) {
            int num = Integer.parseInt(st.nextToken());
            if(map.containsKey(num)) sb.append(map.get(num)).append(" ");
            else sb.append("0").append(" ");
        }
        System.out.println(sb.toString());
    }
}
cs

 

2) 이진탐색 (Lower Bound, Upper Bound)

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
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
 
public class Main {
    public static int N, M;
    public static int[] arr;
    public static StringBuilder sb;
    public static int upperbound(int num) {
        int start = 0;
        int end = arr.length;
        while(start < end) {
            int mid = (start + end) >> 1;
            if(arr[mid] > num) end = mid;
            else start = mid + 1;
        }
        return start;
    }
    public static int lowerbound(int num) {
        int start = 0;
        int end = arr.length;
        while(start < end) {
            int mid = (start + end) >> 1;
            if(arr[mid] >= num) end = mid;
            else start = mid + 1;
        }
        return end;
    }
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine().trim());
        sb = new StringBuilder();
        N = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(bf.readLine());
        arr = new int[N];
        for(int i = 0; i < N; i++) {
            int num = Integer.parseInt(st.nextToken());
            arr[i] = num;
        }
        Arrays.sort(arr);
 
        st = new StringTokenizer(bf.readLine().trim());
        M = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(bf.readLine());
        for(int i = 0; i < M; i++) {
            int num = Integer.parseInt(st.nextToken());
            int up = upperbound(num);
            if(up < 0) sb.append("0").append(" ");
            else sb.append(up - lowerbound(num)).append(" ");
        }
        System.out.println(sb.toString());
    }
}
cs

#문제풀이

HashMap과 이진탐색(Lower bound, Upper bound) 로 풀었다.