-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Length of Valley
65 lines (52 loc) · 1.5 KB
/
Length of Valley
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'''
# Sample code to perform I/O:
name = input() # Reading input from STDIN
print('Hi, %s.' % name) # Writing output to STDOUT
# Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
'''
# Write your code here
def area(arr,n):
stack = []
right_index = [0]*n
index = 0
while index<n:
if len(stack)==0 or arr[stack[-1]]<arr[index]:
stack.append(index)
index += 1
else:
while len(stack)>0:
x = stack.pop()
right_index[x]= index
while len(stack)>0:
x = stack.pop()
right_index[x]= index
return right_index
def l_area(arr,n):
stack = []
left_index = [0]*n
index = 0
while index<n:
if len(stack)==0 or arr[stack[-1]]<arr[index]:
stack.append(index)
index += 1
else:
while len(stack)>0:
x = stack.pop()
left_index[x]= index
while len(stack)>0:
x = stack.pop()
left_index[x]= index
return left_index
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int,input().split()))
left_index = l_area(arr[::-1],n)
left_index = left_index[::-1]
for i in range(n):
left_index[i] = n - left_index[i] + 1
right_index = area(arr,n)
ans = [0]*n
for i in range(n):
ans[i] = right_index[i] - left_index[i] + 1
print(*ans,sep =' ')