본문 바로가기
Problem Solving/Baekjoon

[백준] 29615 알파빌과 베타빌 - Greedy / Java

by graycode 2023. 9. 21.

 문제 링크

 

29615번: 알파빌과 베타빌

$1$번과 $3$번을 바꾸고 $2$번과 $4$번을 바꾸면 $3\,4\,1\,2\,5$가 되어 모든 친구들이 먼저 입주할 수 있다.

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = read(), m = read();

        int[] arr = new int[n];
        for (int i = 0; i < n; i++) arr[i] = read();

        boolean[] flag = new boolean[n + 1];
        for (int i = 0; i < m; i++) flag[read()] = true;

        int i = 0, cnt = 0;
        while (i < m) if (!flag[arr[i++]]) cnt++;

        bw.write(String.valueOf(cnt));
        bw.flush();
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글