https://www.acmicpc.net/problem/11728
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] input1 = br.readLine().split(" ");
int n = Integer.parseInt(input1[0]);
int m = Integer.parseInt(input1[1]);
String[] input2 = br.readLine().split(" ");
String[] input3 = br.readLine().split(" ");
int[] arr1 = new int[n];
int[] arr2 = new int[m];
int[] ans = new int[n+m];
for(int i=0;i<n;i++){
arr1[i] = Integer.parseInt(input2[i]);
}
for(int i=0;i<m;i++){
arr2[i] = Integer.parseInt(input3[i]);
}
Arrays.sort(arr1);
Arrays.sort(arr2);
int second=0;
int index=0;
for(int first =0; first<n;first++){
while(second<m && arr1[first]>arr2[second] ){
ans[index++]=arr2[second++];
}
ans[index++]=arr1[first];
}
while(second<m){
ans[index++]=arr2[second++];
}
bw.write(ans[0]+"");
for(int i=1; i<m+n;i++){
bw.write(" "+ans[i]);
}
bw.flush();
}
}
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
class Main
{
public static void main (String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int[] a = new int[N];
int[] b = new int[M];
for (int i = 0; i < N; i++)
a[i] = sc.nextInt();
for (int i = 0; i < M; i++)
b[i] = sc.nextInt();
int[] ans = new int[N + M];
int indexA = 0;
int indexB = 0;
int indexAnswer = 0;
while (indexA < N && indexB < M) {
if (a[indexA] < b[indexB])
ans[indexAnswer++] = a[indexA++];
else ans[indexAnswer++] = b[indexB++];
}
while (indexA < N) {
ans[indexAnswer++] = a[indexA++];
}
while(indexB < M) {
ans[indexAnswer++] = b[indexB++];
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for (int x : ans)
bw.write(x + " ");
bw.write("\n");
bw.flush();
}
}
'Problem Solving > 투포인터' 카테고리의 다른 글
BOJ15831 준표의 조약돌 (1) | 2024.03.04 |
---|---|
BOJ17609 회문 (0) | 2024.03.04 |
BOJ2118 두개의 탑*R (0) | 2024.03.03 |
BOJ 12891 DNA 비밀번호 (0) | 2024.03.03 |
BOJ2230 수 고르기 (0) | 2024.03.03 |