Showing posts with label SPOJ. Show all posts
Showing posts with label SPOJ. Show all posts

Monday, 28 October 2019

[SegmentTree][SPOJ] ORDERS

ORDERS

Problem statement:

As you are probably well aware, in Byteland it is always the military officer's main worry to order his soldiers on parade correctly. In Bitland ordering soldiers is not really such a problem. If a platoon consists of n men, all of them have different rank (from 1 - lowest to n - highest) and on parade they should be lined up from left to right in increasing order of rank.
Sounds simple, doesn't it? Well, Msgt Johnny thought the same, until one day he was faced with a new command. He soon discovered that his elite commandos preferred to do the fighting, and leave the thinking to their superiors. So, when at the first rollcall the soldiers lined up in fairly random order it was not because of their lack of discipline, but simply because they couldn't work out how to form a line in correct order of ranks. Msgt Johnny was not at all amused, particularly as he soon found that none of the soldiers even remembered his own rank. Over the years of service every soldier had only learned which of the other soldiers were his superiors. But Msgt Johnny was not a man to give up easily when faced with a true military challenge. After a moment's thought a solution of brilliant simplicity struck him and he issued the following order: "men, starting from the left, one by one, do: (step forward; go left until there is no superior to the left of you; get back in line).". This did indeed get the men sorted in a few minutes. The problem was solved... for the time being.
The next day, the soldiers came in exactly the same order as the day before, and had to be rearranged using the same method. History repeated. After some weeks, Msgt Johnny managed to force each of his soldiers to remember how many men he passed when going left, and thus make the sorting process even faster.
If you know how many positions each man has to walk to the left, can you try to find out what order of ranks the soldiers initially line up in?

Input

The first line of input contains an integer t<=50, the number of test cases. It is followed by t test cases, each consisting of 2 lines. The first line contains a single integer n (1<=n<=200000). The second line contains n space separated integers wi, denoting how far the i-th soldier in line must walk to the left when applying Msgt Johnny's algorithm.

Output

For each test case, output a single line consisting of n space separated integers - the ranks of the soldiers, given from left to right in their initial arrangement.

Example

Input:
2
3
0 1 0
5
0 1 2 0 1

Output:
2 1 3
3 2 1 5 4 
 
 

My approach:

There was an approach of mine that worked in $O(n^2 log n)$ time. So bravo I'm a genius. So I read solutions for this question everywhere, then I understood the answer, and eventually it was pretty easy.

So you're at the right most element. It says "1". That means it has one element that is greater than it on the left of it. We have 5 numbers available, 1,2,3,4 and 5. Now, if it were 3, then we would have a 2, as 4 and 5 are definitely in the array. So, no, it must be a 4. As 4 has one element that is greater than it. Cool. Now we can move forward without 4. So we have 1,2,3, and 5.

We are at the 4th position now, which says 0. So, it has no element on the left of it that is greater than it. What such element can be possible? The greatest of those left. Right? That is $(\text{total elements left} - \text{number of elements greater than it})^{th}$ element. That's $4-0 = 4$, that is, the 4th element in the numbers that are left out. That seems to be 5. So we remove it, and we're left with 1,2,and 3.

Again, we are at the 3rd position, which says  2. So, we find the element which has 2 elements greater than in in the remaining elements. That is, $3-2=1$. So we're talking about 1. So, let's put 1 at this position and move forward. So, now we have only 2 and 3 remaining.

You get my point. Right?

So how will you do it optimally? We have to care for the removed elements. We should know that the 4th element after the removal of 2 is 5. So, we can create a segment tree for that, where each number has a 1 assigned to it.

Once we use that number, it's 1 becomes a 0. The parent nodes have the sum of the child nodes. Updation is easy peasy too.

Here is the code:


Tuesday, 22 October 2019

[SegmentTree][SPOJ] BRCKTS

BRCKTS

Question statement:

We will call a bracket word any word constructed out of two sorts of characters: the opening bracket "(" and the closing bracket ")". Among these words we will distinguish correct bracket expressions. These are such bracket words in which the brackets can be matched into pairs such that
  • every pair consists of an opening bracket and a closing bracket appearing further in the bracket word
  • for every pair the part of the word between the brackets of this pair has equal number of opening and closing brackets
On a bracket word one can do the following operations:
  • replacement -- changes the i-th bracket into the opposite one
  • check -- if the word is a correct bracket expression

Task

Write a program which
  • reads (from standard input) the bracket word and the sequence of operations performed,
  • for every check operation determines if the current bracket word is a correct bracket expression,
  • writes out the outcome (to standard output). 

My approach:

Well well well. This one took a lot of time for me to understand how exactly I would get the answer for the current node. Initially I thought in the direction of having a single value for each node, which is a total for the range, in which a '(' is counted as a 1 and ')' as a -1.

Then I thought of having valid and invalid nodes, depending on the values of their children. But no point into going into the wrong approach.

Well what struck me was the point that all we needed to see the brackets which were imbalanced. That is, the ones that are not matched. So, I could keep the track of left unmatched brackets and right unmatched brackets, for a node, for the range to which it corresponds. Cool.

How to maintain this property?

That is, given that we have these values for the left and right children of a node, can we get the values for the parent node?

Let's say that the left child has $L_{l}$ left unmatched brackets, and $L_{R}$ right unmatched brackets. Similarly, the right child has $R_{l}$ left unmatched brackets, and $R_{r}$ right unmatched brackets.

Now when we're merging the ranges of the left and the right children, we can see that the left unmatched brackets of the left child and the right unmatched brackets of the right child cancel out!

Example: L: )((()) has one left and one right unmatched bracket
                R: )(      has one left and one right unmatched bracket
So, while merging, we can do $cancelledBrackets = min(L_{l},R_{r})$, and for the parent node:
$$P_{l} = L_{l}- cancelledBrackets+R_{l}$$
And:
$$P_{r} = L_{r}+R_{r}-cancelledBrackets$$

So, this is how we can build our segment tree.

For updation, we can simply change the 1 to 0 and a 0 to 1, for both the left and right brackets, at the leaf. And then we can update the parents using the method as described above.

Here is my elegant code minions:

Monday, 21 October 2019

[Segment Tree][SPOJ] FREQUENT

FREQUENT 

Here is the problem statement:
You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , ... , aj

Input Specification

The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the query.
The last test case is followed by a line containing a single 0.

Output Specification

For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.




My Solution:

This is a segment tree question. Not because it can only be done via a segment tree, but because I am trying to learn segment trees, and it's amongst the questions I'm practicing. 

So, IMO there are two things one should think about when using segment trees:
1. Merge
2. Update

Merge  here means that assuming you've your answer for the left and the right child of a node, how will you percolate the result up? What intermediate values must you store in the child nodes such that the current node's solution can be reached efficiently?

Update depends upon the context. Do you have a point update or a range update? How will you manage to update the values of the node efficiently for whole of the segtree?

So these are the things I thought.

So, let's say a node contains the maximum occurring number and it's count. Cool cool. Now how does it's parent benefit from this information? It'll have the max count of it's left and right children. Good. But is there any merging required here? That can only be possible if there is an element that is in both the left and the right child. That means that the element is at the right most part of the left child and the leftmost part of the right child. 

But we don't save this information! So let's save that too. The count of the leftmost and the rightmost element of the node. When we merge, we check these too, and update the max element and it's count accordingly.

Now just go till the top and enjoy.

There's no update so no worries. :)


My code :/


Some thoughts around fenwick trees

Some thoughts around fenwick trees References Questions https://codeforces.com/contest/863/problem/E https://www.hackerearth.com/practice/da...