SNAKEEAT - Editorial

why is the code showing TLE…plz explain…link to code → CodeChef: Practical coding for everyone

Can anyone tell me any test case for which my code is failing. I have used sorting and binary search and I tried all combinations of test cases I could think of and all are passing. I got a WA for this code - CodeChef: Practical coding for everyone . Any help is welcome. Thanks

Can anyone please explain why am i getting TLE in my code.
https://www.codechef.com/viewsolution/13706936

I have used suffix sum instead of prefix.I have tried running many test cases but have not been able to spot the problem. I am getting a WA.
https://www.codechef.com/viewsolution/13765159
Can anyone please point out what’s the problem in my solution?

Are there any special test cases we need to consider for the online solution? We use the same approach in our submission (for Java); we also test with our own test cases and it’s seemed ok. But the submission is still WA. Here is our code: link text. Thank you in advance.

why TLE in my code pls help me CodeChef: Practical coding for everyone

I think observation 2 is incorrect. Consider the following case:

Snake lengths: [1,2,3,6,7,9,10,21]

Queries: [10,21]

In the case of query one, K=10, 4 snakes are killed.

In the case of query two, K=21, none of the snakes is killed.

@sharmarahul
liscut and lisrem dont get updated in 3rd case since k1>snakelist[i] for every i
hope it helps

@vivek1_007 In Online Solution, could you please explain why you defined prefix sum as

presum[x]=presum[x−1]+(109−l[x])

instead of

presum[x]=presum[x−1]+l[x]

In case of offline solution how to find largest element smaller than K value of current query which we are processing?

As requested, here is a video editorial on this:

Snackdown: SNAKEEAT - Video Editorial

We use the binary search approach here to solve the problem.

Cheers! :slight_smile:

I haven taken a different approach to solve the problem, the idea:
for each number of eaten snakes compute the length of the smallest remaining snake and store it in an array.
All what needs to be done for a query k is to binary search that array for the fist entry >= k and return n-index.

When submitting the solution I encountered the following strange behaviour:

I get a TLE for the original solution (see my solution).

I produced a WA “on purpose” by not considering the last query in the last testcase (see without last query). BUT I get the WA at a time of 0.23 seconds - hence pretty fast. I came to the last solution but optimizing my first one through many steps and the time when I got the WA on purpose constantly decreased (started with 3 seconds and went through 0.99, 0.3 til 0.23).

Can anyone explain this behavior to me? I can’t imagine a single query causing the time difference from 0.23 to TLE. Am I missing something? Or are the times for WA not representative? But somehow they are, because times decreased as I optimized my solution.

Would be glad for any ideas which give some insight on this!

What is presum[0]?

can somebody give me some inputs on online and offline solutions?

I have sorted array outside the query loop and used only binary search inside, can someone tell me why I am getting TLE? link to my code: CodeChef: Practical coding for everyone

Online solution is a solution in which you are answering a query simultaneously as you are accepting them as input OR you are answering each query before accepting next query as input.

Offline solution is a solution in which you are first accepting all the queries as input, then do something with that and then answer queries in one go.

1 Like

So that means I have always written online solutions. But it’s specifically said “Online” or “Offline” sometimes like in GPD. Does it mean anything? Does it add additional constraints? I can’t possibly see any difference in run time myself.

Offline and Online solutions will usually involve completely different strategies. The GPD question mandated an online solution to make sure the only possible strategy followed is that of persistent data structures. :slight_smile: And yes, if your code got the value of K and immediately printed its answer, before getting the next K value , it was an online solution! :slight_smile:

Common examples of offline processing are questions involving mo’s algorithm.
On a side note GPD can be solved online without persistent data structures.

You are sorting the array repetitively unnecessarily every time after each query, thus TLE.