THEATRE  Movie Theatre Madness
Problem Statement
A group of friends have gone to watch the first day first show of an awesome new movie. However, since they did not book the tickets well in advance, they have ended up with crazy seats. To be more specific, rather than getting seats such that all the friends are seated in the same row, they have ended up with seats such that all of them are seated in the same column! Now this is very inconvenient since they won't be able to chat with each other during the movie or have any kind of fun, but they are okay with this since they'd rather watch the movie like this, than not watch the movie at all.
But there is another problem apart from this. Now we know that all the friends are seated in a single column, one behind the other. Since all of them reached the theatre just in time for the movie, they rushed and occupied the first of the booked seats that they could find. However all the friends have different heights, and due to the lack of planning, there is no guarantee that a shorter person is always seated in front of a taller person. But this would mean that the shorter person would struggle to see the screen throughout the movie !
But the movie has started and it's too late now to do anything.
What you need to do is the following : For every person, find the height of the closest person seated in front of him/her who is blocking his/her view ( that is, the person closest in front with a greater height ). If no such person exists, take this height as 1. Print the product of all such values modulo 1000000007.
Input
On the first line you have a single integer N ( 2<=N<=10^{5} ), the total number of friends.
This is followed by N space separated integers a_{1} a_{2}.... a_{N}, which correspond to the height of the people from back to front.That is, a_{1} is the height of the person seated on the last row, a_{2} is the height of the person seated on the second last row ( just in front of a_{1} ) and so on... uptil a_{N} which is the height of the person seated right at the very front ( 1<=a_{i}<=10^{9} ). Note that all these integers are distinct.
Output
On a single line, output the result. ( For every person, find the height of the closest blocking person. This value is 1 if no such person exists. Print the product of all these values modulo 1000000007 ).
NOTE : By closest blocking person to a_{i} we mean find a_{j}, such that a_{j}>a_{i}, j>i, and ( ji ) is minimum. ( Please look at sample test cases for further clarity )
Example
Input #1:
5
5 2 1 4 3
Output #1:
16
Input #2:
5
9 8 3 5 7
Output #2:
35
Input #3:
10
30 10 50 70 11 60 20 80 31 12
Output #3:
999962375Explanation :
Input #1 :
blockingHeight( 5 ) = 1 ( since 5 is the tallest, no one is blocking him )
blockingHeight( 2 ) = 4
blockingHeight( 1 ) = 4 ( Note that although 3 is also taller that 1, 4 is closer to 1 )
blockingHeight( 4 ) = 1 ( No one blocking 4 )
blockingHeight( 3 ) = 1 ( No one blocking 3 )
Answer=1*4*4*1*1=16
hide comments
sepehragz1382:
20190718 08:10:35
12


sepehragz1382:
20190718 08:06:52
9


rishabh_1997:
20160709 23:44:50
AC in one attempt. 0.08s


Riddhi:
20160526 15:40:19
try this


bunnycoder:
20160114 19:18:00
something new gud question 

Vipul Srivastava:
20150318 19:27:24
Leant something new!! Thanks setter.


zicowa:
20141228 14:50:20
I know that this problem can be solved using stack with O(N) time complexity even it can be solved without using stack ..


Noob:
20141203 08:25:41
Nice question :) 

Ruchir Sharma:
20141112 15:17:16
AC in frst attempt..!! simple O(n) algo wud work.. (Y) 

Kid Algorist:
20141025 13:35:19
DP like approach gives 0.27s AC.

Added by:  Gowri Sundaram 
Date:  20130316 
Time limit:  1s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: ASM32GCC MAWK BC CCLANG CPP14 CPP14CLANG COBOL COFFEE DCLANG DDMD DART ELIXIR FANTOM FORTH GOSU GRV JSMONKEY KTLN NIM OBJC OBJCCLANG OCT PICO PROLOG PYPY R RACKET RUST CHICKEN SQLITE SWIFT UNLAMBDA VB.NET 