https://www.acmicpc.net/problem/15787
📝문제
📝풀이
주어진 수의 범위가 커서 비트 마스킹으로 풀어야 하는 문제이다.
0000...00000 으로 총 21개의 0으로 기차의 상태를 체크한다. ( << 연산을 적용하면 10 부터 시작하기 때문)
명령의 경우
case 1 => 해당 위치에 << 연산을 적용시킨 결과와 or 연산 적용
case 2 => 1번에 not 을 적용시킨 결과와 and 연산 적용
case 3 => << 연산으로 한칸 밀어내고 21개의 1 과 and 연산으로 사이즈 조절
case 4 => >> 연산으로 한칸 밀어내고 not 연산을 적용시킨 ( 11111110) 과 and 연산
나올 수 있는 모든 경우를 카운트한다.
참고) 비트마스크 연산
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.Arrays.stream;
public class Main {
static int n, m;
static int[] trains;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] input = stream(br.readLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray();
n = input[0];
m = input[1];
trains = new int[n + 1];
for (int i = 0; i < m; i++) { // command
input = stream(br.readLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray();
int command = input[0];
int index = input[1];
int seat = input.length == 2 ? 0 : input[2];
switch (command) {
case 1 -> trains[index] |= (1 << seat);
case 2 -> trains[index] &= ~(1 << seat);
case 3 -> {
trains[index] <<= 1;
trains[index] &= ((1 << 21) - 1);
}
case 4 -> {
trains[index] >>= 1;
trains[index] &= ~1;
}
}
}
int ans = (int) stream(trains, 1, n + 1)
.distinct()
.count();
System.out.println(ans);
}
}
'알고리즘,PS > 백준' 카테고리의 다른 글
[BOJ] 백준 [13458] 시험감독 JAVA (0) | 2022.05.30 |
---|---|
[BOJ] 백준 [17136] 색종이붙이기 JAVA (0) | 2022.05.25 |
[BOJ] 백준 [1052] 물병JAVA (0) | 2022.05.18 |
[BOJ] 백준 [17124] 두개의배열 JAVA (0) | 2022.05.14 |
[BOJ] 백준 [19236] 청소년 상어JAVA (0) | 2022.05.11 |