Getting TLE on Codeforces Round #304 (Div. 2), problem: (C) Soldier and Cards

Here is the Problem Link
Soldiers And Cards

My Submission link is My Submission

Please Provide me a Hint. I am getting TLE on Test Case 29. I am applying Brute Force Algorithm.
Help Required.

Thanks! :slight_smile:

Note: In this case(i.e Test case 29) , both of the players stack reach to a situation from where Repetition of states starts i.e after few swaps the stacks become same(as some previous state) but not equal to initial situation as given in Input. so your equate() function always returns 0.and hence TLE.

  • Note you can keep a counter and assign it a upper bound and now if the number of iterations goes beyond upper bound value print -1.
    Hope it helps.
1 Like

Thanks Bro. I got it :slight_smile: