티스토리 뷰
풀이
1) 스위치는 배열에 학생의 정보는 큐에 담는다
2) 큐를 하나씩 빼내면서 배열값 갱신
2-1) 남학생일 경우 : 뽑은 값의 배수는 전부 반전
2-2) 여학생일 경우 : 양쪽으로 검사해서 같으면 반전 후 이어나감 아니면 끝
3) 20개씩 출력
주의사항
1) 20개씩 출력해야함
package com.baekJoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class BJ_S4_1244_스위치켜고끄기 {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokens;
static int N,K, map[];
static Queue<Student> queue = new LinkedList<>();
public static void main(String[] args) throws NumberFormatException, IOException {
input = new BufferedReader(new StringReader(src));
N = Integer.parseInt(input.readLine());
map = new int[N];
tokens = new StringTokenizer(input.readLine());
for(int n=0; n<N; n++) {
map[n] = Integer.parseInt(tokens.nextToken());
}
K = Integer.parseInt(input.readLine());
for(int k=0; k<K; k++) {
tokens = new StringTokenizer(input.readLine());
int gender = Integer.parseInt(tokens.nextToken());
int num = Integer.parseInt(tokens.nextToken());
queue.offer(new Student(gender, num));
}
solve();
for(int n=0; n<N; n++) {
System.out.print(map[n]+" ");
if((n+1)%20 == 0) System.out.println();
}
// 주의사항
}
private static void solve() {
while(!queue.isEmpty()) {
Student front = queue.poll();
if(front.gender == 1) { // 남학생
int num = front.num;
for(int i=0; i<N; i++) {
if((i+1)%num == 0) {
if(map[i] == 0) {
map[i] = 1;
}else {
map[i] = 0;
}
}
}
}
else { // 여학생
int num = front.num-1;
int nl = num;
int nr = num;
if(map[num] == 0) {
map[num] = 1;
}else {
map[num] = 0;
}
for(int i=0; i<N/2; i++) {
if(isIn(--nl) && isIn(++nr) && map[nl] == map[nr]) {
if(map[nl] == 0) {
map[nl] = 1;
map[nr] = 1;
}else {
map[nl] = 0;
map[nr] = 0;
}
}else {
break;
}
}
}
}
}
static boolean isIn(int n){
return (n>=0 && n<N);
}
static class Student{
int gender;
int num;
public Student(int gender, int num) {
super();
this.gender = gender;
this.num = num;
}
@Override
public String toString() {
return "Student [gender=" + gender + ", num=" + num + "]";
}
}
static String src =
"21\r\n" +
"0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0\r\n" +
"2\r\n" +
"1 3\r\n" +
"2 3";
}
후기
어려울점이 없는 단순한 구현문제이다. 하지만 20개씩 출력해야한다는 설명을 못봐서 한번 틀려버렸다.
문제를 똑바로 읽는 습관을 가지자.
'Algorithm' 카테고리의 다른 글
[백준] S5 2422 한윤정이 이탈리아에 가서 아이스크림을 사먹는데 (java) (0) | 2021.01.02 |
---|---|
[백준] S3 1654 랜선 자르기 (java) (0) | 2021.01.02 |
[백준] S5 1436 영화감독 숌 (java) (0) | 2021.01.02 |
[백준] G4 1339 단어 수학 (java) (0) | 2021.01.02 |
[백준] S4 10816 숫자 카드 2 (java) (0) | 2021.01.01 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 객체지향
- react native
- Spring Boot
- 백트래킹
- G5
- 알고리즘
- 자바
- 코딩새내기
- 그리디
- Spring
- react
- S3
- 시뮬레이션
- 문자열
- SWEA
- 백준
- map
- 리액트 네이티브
- 현꾸라지
- laugh4mile
- 리액트
- 우선순위큐
- PriorityQueue
- 구현
- java
- g4
- BFS
- DFS
- S2
- 다익스트라
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함