https://www.acmicpc.net/problem/2579
solved.ac 기준: Class 3, 실버 3
import sys
read = sys.stdin.readline
N = int(read().rstrip())
stairs = [0] +[int(read().rstrip()) for _ in range(N)] # 0 번째 시작점은 인덱싱 편의를 위한 더미
stairs.append(0) # 마지막 계단은 IndexError를 방지하기 위한 더미
# 모두 다 기록하지 않고 필요한 만큼만 갱신하면서 가져가는 tabulation 방식
# (누적 점수, 연속으로 1칸 오른 수)의 튜플이 각 집합의 원소로 들어갈 것임
current = {(stairs[1], 1)} # 1층
next = [(stairs[2], 1), (0, 2)] # 2층
next_next = [(0, 1), (0, 2)]
for i in range(1, N): # 1부터 마지막 바로 전(N-1) 계단까지 방문하면서
for record in current:
# 연속 두 개 계단을 밟지 않은 상태라면
if record[1] == 1:
# 다음 기록에 한 칸 이동 추가
next_record = (record[0] + stairs[i+1], 2)
# 다음 칸 기록의 값 보다 클 경우에만 갱신 (연속 스텝 수 같은 기록만)
if next_record[0] > next[1][0]:
next[1] = next_record
# 다음 다음 기록에 두 칸 이동 추가
next_record = (record[0] + stairs[i+2], 1)
# 다음 다음 칸 기록의 값 보다 클 경우에만 갱신 (연속 스텝 수 같은 기록만)
if next_record[0] > next_next[0][0]:
next_next[0] = next_record
# record 담은 리스트들 갱신
current = next
next = next_next
next_next = [(0, 1), (0, 2)]
# for문이 끝나면 마지막 계단의 기록들이 current에 담겨있을 것임
ans = 0
for record in current:
if record[0] > ans:
ans = record[0]
print(ans)
import sys
read = sys.stdin.readline
N = int(read().rstrip())
stairs = [0] +[int(read().rstrip()) for _ in range(N)] # 0 번째 시작점은 인덱싱 편의를 위한 더미
stairs.append(0) # 마지막 계단은 인덱싱 편의를 위한 더미
# (누적 점수, 연속으로 1칸 오른 수)의 튜플이 각 리스트의 원소로 들어갈 것임
records = [[] for _ in range(N+2)] # 마지막 리스트는 IndexError 방지를 위한 더미
records[0] = [(0, 0)]
for i in range(N):
for record in records[i]:
# 연속 두개 계단을 밟지 않은 상태라면
if record[1] < 2:
# 다음 기록에 한 칸 이동 추가
next_record = (record[0] + stairs[i+1], record[1] + 1)
records[i+1].append(next_record)
# 다음 다음 기록에 두 칸 이동 추가
next_record = (record[0] + stairs[i+2], 1)
records[i+2].append(next_record)
ans = 0
for record in records[N]:
if record[0] > ans:
ans = record[0]
print(ans)
import sys
read = sys.stdin.readline
N = int(read().rstrip())
stairs = [0] +[int(read().rstrip()) for _ in range(N)] # 0 번째 시작점은 인덱싱 편의를 위한 더미
stairs.append(0) # 마지막 계단은 더미
# 모두 다 기록하지 않고 필요한 만큼만 가져가는 tabulation
# (누적 점수, 연속으로 1칸 오른 수)의 튜플이 각 리스트의 원소로 들어갈 것임
current = [(0, 0)]
next = []
next_next = []
for i in range(N): # 0부터 마지막 바로 전(N-1) 계단까지 방문하면서
for record in current:
# 연속 두개 계단을 밟지 않은 상태라면
if record[1] < 2:
# 다음 기록에 한 칸 이동 추가
next_record = (record[0] + stairs[i+1], record[1] + 1)
next.append(next_record)
# 다음 다음 기록에 두 칸 이동 추가
next_record = (record[0] + stairs[i+2], 1)
next_next.append(next_record)
# record 담은 리스트들 갱신
current = next
next = next_next
next_next = []
# for문이 끝나면 마지막 계단의 기록들이 current에 담겨있을 것임
ans = 0
for record in current:
if record[0] > ans:
ans = record[0]