슈콩

[BOJ] 백주 1822 차집합 본문

Algorithms/Baekjoon

[BOJ] 백주 1822 차집합

shukong 2025. 9. 19. 21:03

[문제]

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

 

 

[소스 코드]

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException {
		StringBuilder sb = new StringBuilder();
		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());
		int[] a = new int[n];
		for(int i=0;i<n;i++) {
			a[i] = Integer.parseInt(st.nextToken());
		}
		List<Long> b = new ArrayList<>();
		st = new StringTokenizer(br.readLine());
		for(int i=0;i<m;i++) {
			b.add(Long.parseLong(st.nextToken()));
		}
		Arrays.sort(a);
		Collections.sort(b);
		List<Long> list = new ArrayList<>();
		for(long num : a) {
			if(Collections.binarySearch(b,num)>=0)
				continue;
			else
				list.add(num);
		}
		sb.append(list.size() + "\n");
		for(long num : list) {
			sb.append(num + " ");
		}
		System.out.println(sb);
	}
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 2110 공유기 설치  (0) 2025.09.20
[BOJ] 백준 18869 멀티버스 Ⅱ  (0) 2025.09.20
[BOJ] 백준 14502 연구소  (2) 2025.09.19
[BOJ] 백준 14499 주사위 굴리기  (0) 2025.09.19
[BOJ] 백준 16401 과자 나눠주기  (0) 2025.09.18