UNSQUERS - Editorial

I solved this problem without using segment tree. I just used array and stack, I think the test files was not so strong.
My Solution - CodeChef: Practical coding for everyone

3 Likes

No, you can’t use LIS concept in this question suppose you chose a sub array 1 2 1000 3 4
So for LIS concept the longest sequence will be (1 2 3 4) but for the question the longest will be (1 2 1000) only as u dont have choice for 1000 to take it or not if you encounter a no. Greater than prev you have to take it . Hope it helps

2 Likes

You could have reported in comments section :stuck_out_tongue:

Thanks guys , I got it now :slight_smile:

Welcome Hitesh!

I used range maximum query and binary lifting to solve this problem, the main idea is the following.

If we connect each element to its next greater element with an edge; and those which don’t have a next greater element to a universal root, we get a rooted tree. Observe that the indices of the array act as exit numbers in the rooted tree.

Now the queries become, given the two exit numbers, find the maximum length of a chain.

Now the nodes between two exit numbers contain some(possibly zero) entire subtrees and at most one partial subtree. We use Range Maximum query for the entire subtrees and Binary Lifting for the partial subtrees.

my code: CodeChef: Practical coding for everyone

6 Likes

This can be done with range max segment tree + stack + next greater element.

1 Like

I did exactly this too, was going to post it here but saw your comment. :slight_smile: In my case I used constant of 330, but I suspect many other values work too - it essentially got accepted on a very first attempt of choosing this constant.

Here’s my submission in case anyone wants to take a look - CodeChef: Practical coding for everyone.

1 Like

I did not understand this part, Please someone explain this part.
Btw the problem is very good.

This post was flagged by the community and is temporarily hidden.

@llaki10 I really did not expect anyone using this approach :stuck_out_tongue:
But you have used it ! :slight_smile: :handshake:

For 10 days i was thinking why LIS brute is not passing even for 10.

2 Likes

Yor way of doing problems are amazing. I liked your solution of scalar product tree and this one.

1 Like

Thanks :slight_smile:

@l_returns
In the first query, Chef can choose to visit a contiguous subsequence of mountains with heights (4,2,3,1,5,5). The subsequence that gives the maximum satisfaction is (2,3,1,5)

Then why example input has answer 3 ?

Chef will only not values in increasing order right. So he will write 0 2 3 5 and hence answer is len([2,3,5]) = 3

Got it thank you

Here we are calculating the required output(i.e. max satisfaction) for each and every subarray having size “siz” throughout the array and also for each “siz” we’re using the precomputed output for subarrays having size “siz-1” as max satisf. for subarray [l,r] would be max. of values for [l,r] and [l+1,r] .

1 Like

Thanks bro, I loved ur explanation.

1 Like

My pleasure :smiley: :innocent:.

1 Like