https://www.acmicpc.net/problem/11728
투포인터 이용해서 해결할 수 있는 문제
priority queue를 활용하면 메모리는 더 적게 걸리면서 해결할 수 있음. 하지만 시간이 더 오래..
아 total 안만들고 그냥 바로 출력해도 되는데 어쨌든 두 배열중에 하나는 빨리 끝나니까 while문으로 배열 끝까지 탐색할 수 있도록 해주기!
#include <iostream>
#include <vector>
using namespace std;
int N, M;
vector<int> arr1;
vector<int> arr2;
vector<int> total;
int nidx, midx;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N >> M;
int temp;
for (int i = 0; i < N; i++) {
cin >> temp;
arr1.push_back(temp);
}
for (int i = 0; i < M; i++) {
cin >> temp;
arr2.push_back(temp);
}
while (nidx < N && midx < M) {
if (arr1[nidx] <= arr2[midx]) {
total.push_back(arr1[nidx++]);
}
else {
total.push_back(arr2[midx++]);
}
}
if (nidx < N) {
while (nidx != N) {
total.push_back(arr1[nidx++]);
}
}
if (midx < M) {
while (midx != M) {
total.push_back(arr2[midx++]);
}
}
for (int i = 0; i < total.size(); i++) {
cout << total[i] << ' ';
}
}
'알고리즘' 카테고리의 다른 글
[boj2075/c++] N번째 큰 수 (0) | 2023.05.05 |
---|---|
[boj2599/c++] 짝 정하기 (1) | 2023.05.05 |
[boj4963, boj15652/c++] 섬의 개수, N과 M(4) (0) | 2023.05.02 |
[boj9934/c++] 완전 이진 트리 (0) | 2023.05.01 |
[boj1389/c++] 케빈 베이컨의 6단계 법칙 (0) | 2023.05.01 |