✏️ Algorithm/알고리즘 풀이

백준 11728번 - 배열 합치기 C++

미미누 2022. 2. 6. 19:36

[문제]

[풀이]

정렬

[코드]

#include <bits/stdc++.h>
using namespace std;

int n,m;
int a[1000005], b[1000005], c[10000005];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for(int i=0; i<n; i++) cin >> a[i];
    for(int i=0; i<m; i++) cin >> b[i];
    int aidx=0, bidx = 0;
    for(int i=0; i<n+m; i++) {
        if(bidx == m) c[i] = a[aidx++];
        else if(aidx == n) c[i] = b[bidx++];
        else if(a[aidx] <= b[bidx]) c[i] = a[aidx++];
        else c[i] = b[bidx++];
    }
    
    for(int i=0; i<n+m; i++) cout << c[i] << ' ';
}