[20250724] BOJ / P5 / 막대기 / 권혁준 #535
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/8984
🧭 풀이 시간
25분
👀 체감 난이도
✏️ 문제 설명
길이가 L만큼 떨어진 두 평행한 수직선 T, D가 있다.
두 수직선의 어떤 점 사이를 잇는 선분을 막대기라 하며, 이런 막대기가 N개 주어진다.
막대기의 길이는 택시 거리로 계산한다.
아래 세 조건을 만족하는 지그재그 선의 길이의 최댓값을 구해보자.
🔍 풀이 방법
규칙성이 없어 보이지만, 수직선 T나 D를 기준으로 잡고 정렬하면 DP식을 정의할 수 있게 된다.
막대기들을 수직선 D의 좌표 기준 오름차순으로 정렬한다.
두 개의 DP를 정의한다.
이렇게 DP를 정의하면, 막대기 a = (t, d)에 대해,
H[t] = max(H[t], D[d] + |t-d| + L)
L[d] = max(L[d], H[t] + |t-d| + L)
이 성립한다.
근데 좌표의 범위가 10^9라서 배열 인덱스로 써먹을 수가 없다.
그래서 배열 대신 Map을 써서 큰 수도 key로 쓸 수 있게 해줬다.
이 중 최댓값을 뽑아 출력해줬다.
⏳ 회고
으아아아악