백준 11728번 배열 합치기 문제 풀이
- 정렬되어있는 두 배열 A와 B가 주어진다. 두 배열을 합친 다음 정렬해서 출력하는 프로그램을 작성하시오.
- 투 포인터
- 어떻게 하면 빠르게 할 수 있을까?
- a, b 벡터를 각각 하나씩 만든다.
- 투 포인터로 출력하고 난 다음 포인터를 이동시켰다.
#include <iostream>
#include <vector>
using namespace std;
int N, M;
vector<int> a, b;
void getInput(){
cin >> N >> M;
for(int i=0; i<N; ++i){
int tmp; cin >> tmp;
a.push_back(tmp);
}
for(int i=0; i<M; ++i){
int tmp; cin >> tmp;
b.push_back(tmp);
}
}
void printArr(){
int lp=0, rp=0;
while(lp < N || rp < M){
if(lp >= N){
cout << b[rp++] << " ";
continue;
}
if(rp >= M){
cout << a[lp++] << " ";
continue;
}
if(a[lp] < b[rp]){
cout << a[lp++] << " ";
} else{
cout << b[rp++] << " ";
}
}
}
void solve(){
getInput();
printArr();
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
백준에서 채점하는데 1분이나 걸렸다..
더 좋은 풀이가 있을 것 같다.
인터넷에서 찾아봐야겠다.