The Table Tennis problem in CodeForces was looking actually easier than the Borya’s Diagnosis. However, I have decided to publish it, because I have managed to “lie or cheat” the CodeForces system with it in a cool way. Currently, I am reading the Clean C++ book and sometimes, when you read about “clean” code the ideas to make a dirty solution is somehow really lucrative. Furthermore, in the clean book there are plenty of examples of what we should not do, thus I am getting really enthusiastic to try some of them.
So, a big disclaimer – DO NOT CODE LIKE THIS anywhere. But it was fun, because it passed all the tests of CodeForces – 43 in total and was granted with full points.
This is how the problem looks like:
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
The first line contains two integers: n and k (2 ≤ n ≤ 500, 2 ≤ k ≤ 1012) — the number of people and the number of wins.
The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ n) — powers of the player. It’s guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output a single integer — power of the winner.
So, what is the problem with this quite easy task? Well, first 10^12 is a big number and second, I needed some time to realize that there is a difference between the first player and everyone else. E.g., the first player needs to win N matches vs other players, the second player has to win one less, because he has obviously won his match with the first player (in order to be allowed to win). The second note is easily implemented, but for the first I did not want to use BigInteger, thus I have decided to build a small cheat – I have noticed that the n is up to 500, thus if more than 500 matches are required, the answer is simply the player with the biggest number for power. Thus, I have left integers everywhere and I have tried to embed it with try-catch. And it worked!
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
class Startup { static void Main() { List inputA; List inputB; try { inputA = ReadLineAndParseToList(); inputB = ReadLineAndParseToList(); int playersCount = inputA[0]; int victoriesNeeded = inputA[1]; int winner = -1; if (playersCount < victoriesNeeded) { winner = inputB.Max(); } else { int playersLeft = playersCount; int players = 0; while (playersLeft*2 > 0) { inputB.Add(inputB[players]); if (players==0) { victoriesNeeded++; } else { victoriesNeeded = inputA[1]; } List firstElement = inputB.Skip(players).Take(victoriesNeeded).ToList(); if (inputB[players]==firstElement.Max()) { playersLeft = -1; winner = inputB[players]; } playersLeft--; players++; } } Console.WriteLine(winner); } catch (Exception ex) { inputB = ReadLineAndParseToList(); Console.WriteLine(inputB.Max()); return; } } |
Definitely not a clean code, but quite good enough to get what I was fighting for. Cheers!