aglorithm의 복잡성을 가진 춤 (A dance with an aglorithm's complexity)


문제 설명

aglorithm의 복잡성을 가진 춤 (A dance with an aglorithm's complexity)

저는 댄스 콘테스트에 참가하려고 하는데 내일이 중요한 날입니다! 나는 경연의 n곡의 목록과 순서를 선험적으로 알고 있습니다. 많은 스카우트 끝에 심사위원과 실력을 판단할 수 있어서 목록의 i번째 곡, 즉 score(i)를 춤을 추면 결과를 정확하게 예측할 수 있었습니다.score(i). p>

그러나 i 번째 곡 이후에는 휴식 시간이 필요합니다. 즉, 다음 rest(i) 곡, 즉 i + 1, ..., i 곡은 춤을 출 수 없습니다. + 휴식(i). 내가 춤출 수 있는 노래의 수에 대한 다른 제약은 없습니다. 이상적인 최대 총점과 복잡성을 계산하기 위한 효과적인 알고리즘을 제공하십시오.


그래서 재귀를 사용해야 한다고 생각합니다. 여기서 max(i) = max(i + 1)</코드> 또는 <코드> max(i) = score(i) + max(i + 1 + rest(i)) 그런 다음 모든 단계에서 이 두 가지 중에서 가장 좋은 것을 선택합니다. 누군가 도울 수 있습니까?


참조 솔루션

방법 1:

Recursion would be,

for i: 1 ‑> n
    DP(n) = max(0,(score(i)+DP(i+r)),DP(i+1)) 

where,

r = resting interval
0 is the situation where it is better not to dance at all :)

Edit 1: Adding explanation

Base case is participant doesn't dance so the total score will be 0

Otherwise, he either dances or not on a particular song. So the decision boils down to if he should dance on this song or not so that the ultimate score is maximized.

If he decided to dance, he will gain score(i) and cannot dance for rest(i) so the remaining max possible score will be the value of score(i) + DP(i + rest(i))

If he decides not to dance on this song, the score will be DP(i+1).

Because we want to maximize the score, we pick the max of these two values.

방법 2:

Let there be n songs indexed 0..n‑1. Let Opt(i) be the maximum total score given that we're free to dance starting on song i. The recurrence for Opt is

Opt(n) = 0
Opt(i) = max(Opt(i+1),
             Score(i) + Opt(i + 1 + Rest(i))) for i = 0..n‑1.

Intuitively, we either don't dance song i and get the value of the remaining time, or we do, score song i, rest, and get the value of the time after resting.

This recurrence should be evaluated for i descending, with the previously calculated values cached in an array. The running time is O(n).

(by gsamarasAdityaDavid Eisenstat)

참조 문서

  1. A dance with an aglorithm's complexity (CC BY‑SA 2.5/3.0/4.0)

#dynamic-programming #recursion #Max #algorithm #loops






관련 질문

나머지 숫자의 xor가 0인 부분 집합의 수를 찾습니다. (Find number of subsets, that xor of remaining numbers equals to 0)

재귀 대신 동적 프로그래밍을 사용하여 가장 큰 공통 접미사(자바스크립트) 찾기 (Use dynamic programming instead of recursion to find largest common suffix (javascript))

제거 방법 : java.lang.OutOfMemoryError (how to remove : java.lang.OutOfMemoryError)

가장 효율적인 좌석 배치 (Most efficient seating arrangement)

aglorithm의 복잡성을 가진 춤 (A dance with an aglorithm's complexity)

장애물이 있는 처음부터 끝까지 경로의 수를 계산합니다. (Count the number of paths from start to end with obstacles)

직사각형 필드 내에서 다양한 크기의 직사각형을 효율적으로 배치 (Efficient placement of variable size rectangles within a rectangular field)

기차 또는 버스를 이용할 수 있는 도시 간 최단 경로 DYNAMIC PROGRAMMING (Shortest path between cities that you can use either train or bus DYNAMIC PROGRAMMING)

인덱스가 오름차순으로 정렬되도록 주어진 합계를 갖는 하위 배열의 개수 (Count of sub-arrays with a given sum such that the indices are in ascending order)

2차원 배열의 경로 수 계산(그리드 Traveller) (Count number of paths on 2d-array (grid Traveller))

n개의 볼과 m개의 빈이 주어지고 각 빈이 특정 용량을 가질 때, 몇 개의 조합이 있습니까? (Given n balls and m bins, each bin with a certain capacity, how many combinations are there?)

재귀 호출을 메모할 때 엄청난 효율성 차이 (Drastic efficiency difference when memoizing recursive calls)







코멘트