• 문제 링크
15729번: 방탈출
첫째 줄에 N(1 ≤ N ≤ 1,000,000)가 주어지고 둘째 줄에는 쪽지에 적혀 있는 N자리의 수가 빈 칸을 사이에 두고 주어진다.
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();
boolean[] arr = new boolean[n + 2];
for (int i = 0; i < n; i++) arr[i] = read() == 1;
int cnt = 0;
for (int i = 0; i < n; i++) {
if (arr[i]) {
arr[i + 1] = !arr[i + 1];
arr[i + 2] = !arr[i + 2];
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;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 15786 Send me the money - Greedy / Java (0) | 2023.08.21 |
---|---|
[백준] 19564 반복 - Greedy / Java (0) | 2023.08.20 |
[백준] 1515 수 이어 쓰기 - Greedy / Java (0) | 2023.08.18 |
[백준] 15881 Pen Pineapple Apple Pen - Greedy / Java (0) | 2023.08.17 |
[백준] 25400 제자리 - Greedy / Java (0) | 2023.08.16 |
댓글