SNAKEEAT - Editorial

@jeetu86044 As mentioned in my comment Mo’s algorithm almost always involve offline solutions.

Question – DQUERY

Explanation for mo’s algorithm and a bit of offline queries is given in this blog.

2 Likes

@grajesh you sorting the array repetively for each test in loop for(p=0;p<q;p++).Instead sort the array outside the loop only one time .Hence your solution has time complexity O(q*(nlogn+n))

well i guess u missed to mention that if there are repeatative elements , for eg consider a series of 11 13 13 13 14 and query being 14 then binary search will retutn the mid 13 instaed of the 13 b4 14, so u need to take care of that according to your soln…

hen binary search again on the prefix difference sum array (which will give the number of snakes to be fed to snakes in [prev+1, cur] to make their length at least K) in the region [1, cur]

can someone explain me more clearly this point

What’s wrong with my python code…Any kind of a help is most welcomed
https://www.codechef.com/viewsolution/13774246

@vivek1_007 According to your third solution, which uses some sort of a BST, can you please provide the proof why the solution will fit in TL supposing all the values of K are distinct ?

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?