Help in OA problem

Revision en1, by Pirate_King, 2023-11-07 23:01:00

I'll keep it short.

Bob has an element x and Alice has a permutation p of size n.

Alice goes through the permutation sequentially and asks Bob what is the relation between x and p[i] .

if p[i] < x + 0.5 Bob replies '<' otherwise Bob replies '>'.

Ex: x = 2 and p = [1,3,2] gives us a string "<><" .

Now Alice is smart and does not ask unnecessary questions.

Ex: x = 2 and p = [3,4,1,2] will gives us a string "><<"

Here Alice ignores the 2nd element because in first question she determined that 3 > x+0.5 so of course 4 is also going to be greater than x+0.5 .

Let c be the number of times "><" occurs as a substring in our resultant string. What will be the sum of all C's for x in range 0 to n ?

I did a brute force O(n^2) solution using stack but as expected got TLE in some tc's as n<=2*1e5. Can someone please guide me towards an efficient solution

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English Pirate_King 2023-11-07 23:07:00 88
en1 English Pirate_King 2023-11-07 23:01:00 899 Initial revision (published)