PROBLEM LINK:Author: Lewin Gan DIFFICULTY:Medium PREREQUISITES:Linearity of expectation PROBLEM:Find the expected value of an optimal strategy for a game involving cards and tokens. QUICK EXPLANATION:The main observation is that all strategies are optimal strategies; all strategies will lead to the same expected value. EXPLANATION:Using the observation above, we get a really simple solution. Let's play all red tokens, then all blue tokens. By linearity of expectation, we can get the total expectation to be $$p * (r / (r+b)) + (r+bp) * (b / (r+b))$$. Intuitively, you can guess this by noticing that the cards are played randomly. We can prove this more formally with induction. Consider a O(rbp) dp solution as follows. Let $f(r,b,p)$ denote the expected value given we have r red cards, b blue cards, p red tokens, and r+bp red tokens. We claim that $$f(r,b,p) = p * (r / (r+b)) + (r+bp) * (b / (r+b)) = (p*r + (r+bp)*b) / (r+b)$$. We prove this by inducting on r+b. We can check that the base cases f(1,0,1), f(0,1,1), f(1,0,0) and f(1,0,1) all make sense (i.e. f(1,0,1) = 1, since we have a red token and red card, so we get 1 point). Now, for the inductive case, consider two cases: we either play a red token or blue token. Play a red token: $$f(r,b,p) = (f(r1,b,p1) + 1) * (r / (r + b)) + (f(r,b1,p1)) * (b / (r + b))$$ $$ = (((((p1)(r1) + (r+bp)*b) / (r+b1) + 1) * r + ((p1)*r + (r+bp)*(b1)) / (r+b1)) * b) / (r+b)$$ This is a bit hard to simplify, but we can use wolfram alpha here. Thus, we get $$ = (p*r + (r+bp)*b) / (r + b)$$ Play a blue token: $$f(r,b,p) = (f(r1,b,p)) * (r / (r + b)) + (f(r,b1,p) + 1) * (b / (r + b))$$ $$ = ((((p(r1) + (r+bp1)*b) / (r+b1)) * r + (((p*r + (r+bp1)*(b1)) / (r+b1)) + 1) * b) / (r+b)$$ This can be simplified with wolfram alpha again here. Thus, we get $$ = (p*r + (r+bp)*b) / (r + b)$$. Thus, in both cases, we get the same expected value, so this completes the induction step. Thus, we've proven this formula works. AUTHOR'S AND TESTER'S SOLUTIONS:
This question is marked "community wiki".
asked 19 Mar, 20:23

alternately this can be solved in O(R + B) by keeping the expected number of Red and Blue Cards and at each step choosing the one which is higher in value and subtracting the probability of a card being chosen from the expected value. code answered 20 Mar, 00:52
Well, we already showed that every strategy is an optimal strategy, so that's why this greedy solution works :) In fact, it doesn't matter what card you play, you can choose anything you have left available.
(20 Mar, 00:53)
I used this approach.
(20 Mar, 00:59)

@rajat1603 I had the same thought,but couldn’t prove it mathematicaly why this strategy would be the best.Can you elaborate? answered 20 Mar, 01:07
1
This strategy is the best because all strategies are equally good. The proof is in the statement above (by induction). For more intuition, you can see that your moves are all fixed but cards are randomized, meaning there isn't a way for you to "beat" the randomness in this case.
(20 Mar, 03:29)

This link would be helpful in understanding the Linearity of Expectation : https://brilliant.org/wiki/linearityofexpectation/ answered 20 Mar, 19:33

@rajat1603 i had attempted the question by keeping the values of r and b and not their expected value ,can you explain why should we keep the expected value of the variables? answered 25 May, 17:55
