알고리즘/백준 알고리즘

1244. 스위치 켜고 끄기

황성안 2021. 4. 8. 23:22
728x90
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
#스위치는 8개
# 1은 on 0은 off
# 성별과 받은수에 따라 방식이다름
# if 남학생은 3번 을받으면 6번 상태바꾸기
# if 여학생 좌우 대칭이면서 가장 많은 스위치 구간 상태 모두 바꿈 구간에 석한 스위치는 항상 홀수
 
 
 
 
#첫 줄에는 스위치 개수 switch <= 100
switch = int(input())
#스위치 상태
switch_con = list(map(int, input().split()))
#학생수 students <= 100
students = int(input())
 
# 성별 1 = 남학생 2= 여학생 ,#학생이 받은수
for i in range(students):
    gender, switch_num = map(int, input().split())
 
    if gender == 1: # 남학생이라면?
        for i in range(1, (len(switch_con)// switch_num)+1):
            if switch_con[(switch_num * i) - 1] == 0: # 만약 0이라면 1로 바꿔준다
                switch_con[(switch_num * i) - 1] = 1
 
            else:
                switch_con[(switch_num * i) - 1] = 0 # 만약 1이라면 0으로 바꿔준다
 
 
    if gender == 2: # 여학생 이라면
        # 구간을 찾아 바꿔준다.
        if switch_con[(switch_num - 1)] == 0:
            switch_con[(switch_num - 1)] = 1
        else:
            switch_con[(switch_num - 1)] = 0
        left = switch_num - 2
        right = switch_num
        while left >= 0 and right < switch and switch_con[left] == switch_con[right]:
            if switch_con[left] == 0:
                switch_con[left], switch_con[right] = 1, 1
            elif switch_con[left] == 1:
                switch_con[left], switch_con[right] = 0, 0
            left -= 1
            right += 1
            if left < 0 or right >= switch:
                break
 
cnt = 0
value = ''
for i in range(switch):
    value += (str(switch_con[i])+ ' ')
    cnt += 1
    if cnt == 20:
        print(value)
        value = ''
        cnt = 0
if len(value) != 0:
    print(value)
 
 
cs
728x90