Submit | All submissions | Best solutions | Back to list |
TWENDS - Two Ends |
In the two-player game “Two Ends”, an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. The player whose cards add up to the highest number wins the game. Now one strategy is to simply pick the card at the end that is the largest — we’ll call this the greedy strategy. However, this is not always optimal, as the following example shows: (The first player would win if she would first pick the 3 instead of the 4.)
3 2 10 4
You are to determine exactly how bad the greedy strategy is for different games when the second player uses it but the first player is free to use any strategy she wishes.
Input
There will be multiple test cases. Each test case will be contained on one line. Each line will start with an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may assume that n is no more than 1000. Furthermore, you may assume that the sum of the numbers in the list does not exceed 1,000,000.
Output
For each test case you should print one line of output of the form:
In game m, the greedy strategy might lose by as many as p points.
where m is the number of the game (starting at game 1) and p is the maximum possible difference between the first player’s score and second player’s score when the second player uses the greedy strategy. When employing the greedy strategy, always take the larger end. If there is a tie, remove the left end.
Example
Input: 4 3 2 10 4 8 1 2 3 4 5 6 7 8 8 2 2 1 5 3 8 7 3 0 Output: In game 1, the greedy strategy might lose by as many as 7 points. In game 2, the greedy strategy might lose by as many as 4 points. In game 3, the greedy strategy might lose by as many as 5 points.
Added by: | Camilo Andrés Varela León |
Date: | 2007-07-26 |
Time limit: | 0.100s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ERL JS-RHINO NODEJS PERL6 VB.NET |
Resource: | East Central North America 2005 |
hide comments
|
||||||||||||||
2015-08-02 15:04:32 ram
Easy DP!! My 50th on SPOJ :) :) !! |
||||||||||||||
2015-07-22 06:26:54 PRIBAN91
Very nice problem! |
||||||||||||||
2015-06-30 13:56:15 mayank
Height of stupidity, thinking that I memoized, but did not, and then clueless about TLEs :P -_- |
||||||||||||||
2015-06-22 21:23:45 ASHU
Is removing left end means 4 3 2 10 4 After removing left end 4 2 10 4 Or 4 10 4 |
||||||||||||||
2015-06-10 14:01:24 Nitesh Tiwari
Isn't the result for the first test case should be 9, 16- (23-16) =9 ,Hows's it 7. Also for the last test case I'm getting 9 instead of 4. Someone please varify how's that, 7 and 4. Thanks. |
||||||||||||||
2015-06-08 07:47:14 :.Mohib.:
Learned a lot.... nice que!! |
||||||||||||||
2015-05-30 16:42:58 Rushikesh Potdar
is complexity is less than ( queries*O(N^2)) Last edit: 2015-05-30 16:44:27 |
||||||||||||||
2015-04-14 05:22:44 forma
Could anyone explain why in a case of tie we should always remove the left end ? why not the right! |
||||||||||||||
2015-04-07 14:31:20 Aadil Ahmad
"If there is a tie, remove the left end." :P :P Last edit: 2015-04-07 14:31:40 |
||||||||||||||
2015-04-02 19:43:50 kunaal
AC in one go!! :) |