HMAPPY1 - EDITORIAL

My solution in Python(link) basically uses the alternative implementation describe in the trailing paragraph above, finding the “main” and “spare” runs of 1 s on a circular view of the data array.

Since the query limit is significantly bigger than the data array, I derive answers for all positions of the array, which gives a simple profile that dips down in the main run of 1 s. Then I can report out from a simple mark that is updated to keep track of where the break in the circle is. No rotation of the array is needed to derive this.

1 Like

Also can be done using brute force Codechef November Challenge 2018 | Appy Loves One ( HMAPPY1 ) - YouTube

2 Likes

Question can be done in O(N+Q) by simply precomputing the first and second largest subsequence of 1’s.
Store the starting index and length of largest and second largest subsequence initailly itself. Then while processing the queries, simply check the conditions that whether the largest subsequence is in “rotation process”, if it is, only then you have check the second largest subsequence and parts of the the largest subsequence(since it has been divided into 2 parts due to rotation). Thus each query takes O(1) as we already have the indices of the largest and second largest subsequence and lengths of both parts (if largest subsequence is under “process”) can be obtained directly using them.

My solution- click here

6 Likes

My approach same as @abhi2402 ,but still i am getting WA in 4 test case.
Anybody help me ,what’s wrong in my code??.
This is my code: CodeChef: Practical coding for everyone

Thanks advance.

any reviews for this approach
I consider connected components and then update them as per query and return max of sizes of connected component.

=> https://www.codechef.com/viewsolution/21573122

1 Like

https://www.codechef.com/viewsolution/21550313
did Without segment tree.
Much easier to understand and short program.

I used the same approach as @abhi2402 but it wasn’t able to pass the 15th sub-task. What could be the reason? Here’s my code : CodeChef: Practical coding for everyone

i have tried this with Segment Tree as per the editorial,
i have build the segment tree for 2*N , but i am stuck at query part, can’t figure out how to find the answer when the answer is the sum of suffix of left child and prefix of right child and not the max of both child;
someone guided me to return a node rather than the answer for each query, i tried it but can’t get the idea how to return the node.
Here’s my code : https://ideone.com/vG4Xo3

can anyone help me with this, and if possible can update the query function.

please someone help me find error in my code…thanks in advance

code link

without segment tree / set …
( like a few others in the posts above) my solution (simple to understand as well) CodeChef: Practical coding for everyone

It’s a little late but I think the solution to this problem is relatively simpler than described in the editorial. Ofcourse other approaches mentioned are also great.

I simply pre-calculated and hashed the sequences of 1’s and their sizes and beginning indexes. Sorted them so I get the largest sequences on one side.

Then I pre calculated the result for each N, by simply checking if the pointer is outside, inside or at the edge of the largest sequence, the second largest and so on…

The code is a little messy but the approach is very simple

https://www.codechef.com/viewsolution/21479865

1 Like

loved the editorial , was struggling a bit to fit segment tree in there .I know of the other approaches now but this is what I neeeded

https://www.codechef.com/viewsolution/21524883

Idea Behind: I made a new vector in a way that it contains number of consecutive 1’s and 0’s

1 1 1 0 0 0 0 1 1

the vector would be: 3 -3 2 (1’s by positive value and 0’s by negative sum)

i kept the maximum abs(maximum sum) till now and used Deque and BANG! Solved

I hope my solution will help you understand

1 Like

It can be done in O(N+Q) as well. My Soln.

2 Likes

Note that my competition answer didn’t implement the answer array, but it makes the code a bit tidier and there is a small timing advantage.

Segment tree is louv XD

@savaliya_vivek I tested your code locally and found that your method to find 1st Max ans 2nd Max is having bugs. Check out this Solution to find your error. rest is fine.

Even though you are using deque, whenever there is a query of ‘?’ type, your code takes O(N) time for getting the largest subsequence. For query of type ‘!’, your code does shifting in O(1) time as you are using deque, but if there are lots of query of type ‘?’ your code can take O(N*Q) time which easily exceeds the time limit. You can check my answer above for doing the problem in O(N+Q).

1 Like

Rotation was just there to explain it carefully.

Well, that’s the alternate Implementation.