URJC2_B - Playing Darts

no tags 

Lucy and her friends are heading to the bar to play darts, the forever fierce battle between drunk people to show who’s the best at throwing some sharpened darts into a board. Seems like a lot of fun.

As crazy as it may appear, Lucy and her friends have mad skills when playing darts, as they may all win flawlessly (and this is not fun at all), they have decided to challenge themselves a little bit with some luck. They will draw from a bowl N numbers ranging from 1 to 60 and then they will try to reach 0 at a standard game of 301.

When playing 301, you start your score at 301 points, for each turn you have three throws, for each throw you will substract the value thrown to the current value you have, if, for some reason you exceed the score you must get to be at 0 points, nothing you’ve done at that turn will be taken into count. If you get to 0 at some point of the game, you win. After your turns ends, the other player’s start its throws

For instance, if you have 32 points left on your board to get to 0 and you throw 2 darts at 16, you win, but if you throw a dart at 17 and 16 (thus earning 33 points), you reset yourself to 32 again and your turn is immediately over, and if you score something like 8, 8 and 12 (earning 28 points), next turn you will have to make 4 points to win.

Lucy and her friends want to know who’s going to be the expected winner at the darts game, because if that person does not win, he or she will pay all the beers!

Input

The first line contains an integer T, which specifies the number of test cases. Then, will follow the descriptions of T test cases.

For each test case you will have an integer N and K representing, respectively, the number of players and the number of scores they will have to make on the board. Then, N lines will follow with K integers separated by a single space denoting the score Kj that a player Ni must make on that turn.

For simplicity, we will assume that they can score any number between 1 and 60 (inclusive) regardless of the board points configuration and that after they throw K darts, they will stop throwing and, if no player has won yet, declare a tie.

Output

For each case you must print the number (1-index based) of the player that is expected to win the standard game of 301 respecting the order of the input, if there is no player that wins the game after throwing all of its darts, print ”TIE” (without quotes)

Example

Input:
3
3 4
60 60 60 58
60 60 60 60
60 59 58 57
3 9
60 60 60 60 40 10 10 1 60
10 20 30 40 50 60 60 60 60
60 60 60 60 60 1 60 60 60
3 9
60 60 60 60 50 10 5 1 50
60 60 60 60 40 10 5 1 5
60 60 60 60 60 60 60 60 60

Output:
TIE
3
2

Constraints

• 1 ≤ N ≤ 100

• 1 ≤ K ≤ 301

• 1 ≤ Ki ≤ 60

Explanation of the third case: at the end of the first round, player 1, 2 and 3 scored 180 points, at the second round, player 1 scored 120 (1 points left), player 2 scored 110 (11 point left) and player 3 scored 180 points (131 left as it got busted), by the next round, player 1 bust out with one throw at 5 points and then, player 2 wins by scoring in three throws 5,1,5 (earning 11 points).


hide comments
Simes: 2019-11-20 09:39:31

60 60 60, 30 30 30, 60 10 15, 20 25 30
This player busts on his 7th dart, i.e. the 1st dart of his 3rd turn. His turn ends immediately, so when does he throw the scores 10 and 15? Are these the 1st and 2nd darts of his 4th turn? Or are 10 and 15 forfeited, and his 4th turn starts with 20?

Edit: I think the answer below answers this question too. The 4th turn starts with scores 10 and 15.

Last edit: 2019-11-21 12:52:38
david_8k: 2017-04-07 11:30:03

@mahmud2690: I think the statement is clear in explaining that the darts must be thrown at order. However, I'll modify it in order to be specific about it.

@pvsmpraveen: No, you can't reuse darts, if you have, at some point, darts {1,2,3,4,5,6} and you succeed in throwing {1,2,3} but fail at throwing {4}, your next throw will be {5,6,null} (null being the end of the list).

pvsmpraveen: 2017-04-06 16:26:27

What i meant was if i have darts in the order {1,2,3,4,5,6} i can throw darts from set {1,2,3} in round1 if i understood correctly because of only 3 per turn and if i exceed 301 it resets to 0....and this statement "Whatever you did in that turn does not count" , can we reuse that dart again? , for example if i throw dart no {2} and it exceeded 301, can i go to next set of darts {4,5,6} or except {2} the other set {1,3,4} comes? or can i reuse {2} again?

Make it clear.

mahmud2690: 2017-04-06 11:24:14

Can you make the statement clearer? Is it possible to use any subset of darts?

david_8k: 2017-04-06 11:03:45

@pvsmpraveen I don't understand your statement, you must respect the order of the thrown darts. If you ever exceed 301 points during the turn, whatever you did in that turn does not count. Hope this helps.

pvsmpraveen: 2017-04-06 09:21:17

if i have darts positioned at { 1, 2 3, 4,5,6 } i can choose anything in {1,2,3} in round1 if i choose {2} and i exceed the score resetting to 0, in round 2 will i have {1,3,4} again or {4,5,6}?

Last edit: 2017-04-06 16:26:41

Added by:david_8k
Date:2017-03-29
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:Own Problem