-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ1244.java
80 lines (64 loc) · 2.29 KB
/
BOJ1244.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* 백준 1244번 - 스위치 켜고 끄기
*
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ1244 {
static int nOfSwitch, nOfStudent;
static int[] switches;
static int[][] student;
public static void main(String[] args) throws IOException {
// Input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
nOfSwitch = Integer.parseInt(br.readLine());
switches = new int[nOfSwitch+1];
st = new StringTokenizer(br.readLine());
for (int i=1; i<=nOfSwitch; i++) {
switches[i] = Integer.parseInt(st.nextToken());
}
nOfStudent = Integer.parseInt(br.readLine());
student = new int[nOfStudent+1][2];
for (int i=1; i<=nOfStudent; i++) {
st = new StringTokenizer(br.readLine());
student[i][0] = Integer.parseInt(st.nextToken()); // 남학생=1, 여학생=2
student[i][1] = Integer.parseInt(st.nextToken()); // 학생이 받은 스위치 개수
}
// Switching
for (int i=1; i<=nOfStudent; i++) {
if (student[i][0] == 1) doMan(student[i][1]);
else doWoman(student[i][1]);
}
// Output : 스위치 상태 - 한 줄에 (최대)20개 출력
for (int i=1; i<=nOfSwitch; i++) {
System.out.print(switches[i] + " ");
if (i % 20 == 0) System.out.println();
}
}
// 남학생 -> 받은 수의 배수인 스위치의 상태를 바꿈
private static void doMan(int n) {
for (int i=n; i<=nOfSwitch; i+=n) swap(i);
}
// 여학생 -> 받은 수를 중심으로 가장 큰 대칭을 이루는 구간전체를 바꿈
private static void doWoman(int n) {
int cnt = 0;
while(true) {
if (n-cnt>=1 && n+cnt<=nOfSwitch && switches[n-cnt] == switches[n+cnt]) {
cnt++;
}
else break;
}
if (cnt == 0) swap(n);
else {
cnt--;
for (int i=n-cnt; i<=n+cnt; i++) swap(i);
}
}
private static void swap(int idx) {
if (switches[idx] == 1) switches[idx] = 0;
else switches[idx] = 1;
}
}