Updated:

1. 문제 링크

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

2. 사용 알고리즘

정렬

3. 풀이

머지 소트의 머지 부분 구현

4. 소스 코드

4-1. C++

https://github.com/dev-aiden/problem-solving/blob/main/boj/11728.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int arr[2000003], arr1[1000003], arr2[1000003];

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n, m; cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> arr1[i];
    for (int i = 0; i < m; ++i) cin >> arr2[i];
    int i = 0, j = 0, k = 0;
    while (i < n && j < m) {
        if (arr1[i] < arr2[j]) arr[k++] = arr1[i++];
        else arr[k++] = arr2[j++];
    }
    while (i < n) arr[k++] = arr1[i++];
    while (j < m) arr[k++] = arr2[j++];
    for (int i = 0; i < n + m; ++i) cout << arr[i] << " ";
    cout << "\n";
    return 0;
}

4-2. JAVA

https://github.com/dev-aiden/problem-solving/blob/main/boj/11728.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    static int arr[] = new int[2000003];
    static int arr1[] = new int[1000003];
    static int arr2[] = new int[1000003];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; ++i) arr1[i] = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < m; ++i) arr2[i] = Integer.parseInt(st.nextToken());
        int i = 0, j = 0, k = 0;
        while (i < n && j < m) {
            if (arr1[i] < arr2[j]) arr[k++] = arr1[i++];
            else arr[k++] = arr2[j++];
        }
        while (i < n) arr[k++] = arr1[i++];
        while (j < m) arr[k++] = arr2[j++];
        StringBuilder sb = new StringBuilder();
        for (i = 0; i < n + m; ++i) sb.append(arr[i]).append(" ");
        sb.append("\n");
        System.out.println(sb);
    }
}

Updated:

Leave a comment