Блог пользователя Jarjit_sigh

Автор Jarjit_sigh, история, 5 лет назад, По-английски

I have faced this problem in my unofficial national team selection test and i have been trying to solve it for days to no avail... Hope someone could help me :(

Given an integer array of length n (n <= 100000). Alberto and Brendaly play a game on it. On each turn the player picks a number from one of the 2 ends of the sequence. Alberto goes first. Both players wants to maximize their total score by the end of the game. If they both play optimally, whats the total score Alberto gets? all elements in array are not larger than 1e9.

Thank you!

  • Проголосовать: нравится
  • +50
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

Auto comment: topic has been updated by Jarjit_sigh (previous revision, new revision, compare).

»
5 лет назад, скрыть # |
 
Проголосовать: нравится -40 Проголосовать: не нравится

Its a pretty standard dp problem.You will find the exact same problem in dp set of leetcode. I explain a bit.All you have to do is to while its Alberto's turn you will try to maximize your answer while at Brendaly turn you will try to minimise your score .

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +27 Проголосовать: не нравится

Afaik there is a n*logn solution for this prob, but i never got around to actually reading it. sorry

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

Constraints for a[i]?

»
5 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +2 Проголосовать: не нравится

I was about the comment the interval dp solution , but then I noticed the constraints . If anyone knows a better solution than O(n^2) it'd helpful . But here goes an O(n^2) dp solution let's score_a be the maximum score that alberto can get and score_b be maximum score that brandely can get . If both play optimally alberto wants to maximize the score_a - score_b , while brandely wants to minimize it ( i.e maximize score_b - score_a ) now let dp[l][r] be the maximum value of score_a — score_b while playing on the range [l,r] if this range contains only one element ( i.e l==r ) the dp[l][[r] = array[l] else Alberto the choose either the first element from the range in which case brandely gets to choose from the range [l+1,r] and alberto can choose the last element then brandely choose from [l ,r-1] thus the dp transition is:

dp[l][r] = max( a[l] &mdash; dp[l+1][r] , a[r] &mdash; dp[l][r-1] ) ;

and the result would (sum + dp[1][n])/2 . Since sum contains the value of score_a + score_b .

»
5 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +58 Проголосовать: не нравится

Solution in $$$O(n)$$$ (proved by AC, I'm not so sure about the correctness)
If $$$a_{i-1} \leq a_{i} \geq a_{i+1}$$$, and a player chooses the index $$$i \pm 1$$$, the next two optimal moves are choosing respectively the indices $$$i$$$ and $$$i \mp 1$$$. Hence, these values can be replaced with a single value $$$a_{i-1} - a_i + a_{i+1}$$$. Repeat until you don't have such "peaks". Then, choosing greedily the maximum value works.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Is n even?

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +17 Проголосовать: не нравится

I believe this problem was covered in an Algorithm's Live video.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

There is an algorithm named "Termity" that can solve this in $$$O(N)$$$ time or $$$O(N log N)$$$ time. Link to the paper is: https://www.mimuw.edu.pl/~idziaszek/termity/termity.pdf