Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Let Tickets be a random access list
- Let Rating(i) be the user rating of the ith song
- Initialization:
- Tickets[0] = 0
- For (i=1 to SongNumber-1)
- Tickets[i] = (L[i-1] + Rating(i-1))
- RatingSum = Tickets[SongNumber-1] + Rating(SongNumber-1)
- TicketSum = RatingSum
- Select Next Song:
- rnd = Random from 0 to TicketSum-1
- // Use binary search to find the next song
- left = 0
- right = SongNumber-1
- While (left < right) Do
- i = left + ((left + right) / 2)
- If (Tickets[i] > rnd)
- right = i-1
- Else
- left = i
- If (left == right)
- next_song = i
- // Update the ticket sum
- If (next_song == SongNumber-1)
- TicketSum = TicketSum + TicketSum - Tickets[next_song]
- Else
- TicketSum = TicketSum + Tickets[next_song+1] - Tickets[next_song]
- TicketSum = TicketSum + RatingSum
- // Update the individual tickets
- For (i=1 to next_song)
- Tickets[i] = Min(Tickets[i] + Rating(i-1) - Tickets[i-1] , MaxTickets) + Tickets[i-1]
- If (next_song < SongNumber-1)
- Tickets[next_song+1] = Tickets[next_song]
- For (i=next_song+2 to SongNumber-1)
- Tickets[i] = Min(Tickets[i] + Rating(i-1) - Tickets[i-1] , MaxTickets) + Tickets[i-1]
- Return next_song
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement