Блог пользователя vsanjay_nitdgp

Автор vsanjay_nitdgp, история, 9 лет назад, По-английски

Sir..I tried the below question ..I got the correct answer for all test cases.but I got TLE due to constraints..then I saw some users solutions..they did that problem with a formula...but I wonder what is that formula....could any one explain how to get AC with in time limit..and explain that formula clearly..how did they get it. https://www.codechef.com/PROCON15/problems/PRCNSR1

Thanks

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
9 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Well it is obvious that the greedy strategy of always taking the largest number you can will give the largest amount of unused tickets. Now implementing that strategy in O(K) is too slow. There might be some direct formula but in my solution I used simply binary search.

You can notice that the tickets you use will have values of {K, K-1, K-2 ... K-p, A}. That is, you'll use some amount of the largest consecutive tickets and then at most one other ticket. So you can binary search for p, that is binary search for the amount of largest consecutive tickets you'll take. Once you've fixed that it is easy to see whether you can get sum of N with at most one additional ticket. It is obvious that you are looking to minimize p so binary search works nicely with complexity of O(log K)