<문제 링크>
https://www.acmicpc.net/problem/1244
<정답 코드>
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
|
import sys
input = sys.stdin.readline
N = int(input()) # 스위치 수
switch = [-1] + list(map(int, input().split()))
M = int(input()) # 학생 수
for _ in range(M):
student, num = map(int, input().split())
if student == 1: # 남학생일 경우
for i in range(num, N+1, num):
switch[i] = 1 - switch[i]
elif student == 2: # 여학생일 경우
switch[num] = 1 - switch[num]
left, right = num - 1, num + 1
while left > 0 and right <= N and switch[left] == switch[right]:
switch[left] = 1 - switch[left]
switch[right] = 1 - switch[right]
left -= 1
right += 1
for i in range(1, N+1):
print(switch[i], end=" ")
if i % 20 == 0:
print()
|
cs |
남학생일 경우 주어진 수(num)의 배수
-> (line 11) range에서 i를 num부터 num씩 증가시키기
에 위치하는 스위치 0이면 1로, 1이면 0으로 변경
-> (line 12) 1에서 현재 switch[i] 뺀 값으로 업데이트
여학생은 자기가 받은 수와 같은 번호가 붙은 스위치를 중심으로,
좌우가 대칭이면서 가장 많은 스위치를 포함하는 구간을 찾아서
-> (line 16) left와 right가 인덱스를 벗어나지 않으면서 switch[left]와 switch[right]가 일치
그 구간에 속한 스위치의 상태 모두 바꿈
-> (line 17,18) 1에서 현재 switch[i] 뺀 값으로 업데이트
어려운 구현은 아니었지만, 위와 같은 아이디어로 간단하게 구현할 수 있다.
'프로그래밍 > Python' 카테고리의 다른 글
백준_python 1926번 그림 (DFS, BFS) (0) | 2024.08.04 |
---|---|
백준_python 5525번 IOIOI (문자열) (0) | 2024.08.03 |
백준_python 1149번 RGB거리 (DP) (0) | 2024.07.30 |
백준_python 11053번 가장 긴 증가하는 부분 수열 (DP) (0) | 2024.07.29 |
백준_python 23057번 도전 숫자왕 (set, combinations) (0) | 2024.06.19 |