As the Formula One Grand Prix was approaching, the officials decided to make the races a little more interesting with a new set of rules.
According to the new set of rules, each driver will be given a vehicle with different height and the driver with maximum SIGHT would win the race.
Now, SIGHT of a driver is defined by ( X * P ) % 1000000007, where
X = number of drivers he can see in front of him + number of drivers he can see behind him
P = position of the driver in a given scenario ( index of the driver array is 1-N indexed )
As all the drivers are moving in a straight line, a driver i cannot see beyond another driver j if height of j ≥ height of driver i.
INPUT
First line of the input contains t, the number of test cases. The 1st line of each test case consists of a single integer n, the number of drivers. Second line contains n space separated integers H[1], H[2], H[3]...H[n] denoting the heights of the drivers 1, 2, 3....n.
OUTPUT
Output for each test case should be a single line displaying the index of the winning driver.
In case of ties, display the driver with minimum index.
CONSTRAINTS
0 ≤ t ≤ 50
1 ≤ n ≤ 10 ^ 5
0 ≤ H[i] ≤ 10 ^ 6
SAMPLE INPUT
2
5
4 1 2 1 4
5
5 1 2 4 1
SAMPLE OUTPUT
5
4
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to understand the problem: There is a line of drivers (positions 1 to n). Each driver has a height H[i]. SIGHT is defined as (X * P) % 1000000007, where X = number of drivers he can see in front of him + number of drivers he can see behind him, and P = position (index). So we need to compute for each driver the total number of drivers he can see both forward and backward.
Definition of visibility: "a driver i cannot see beyond another driver j if height of j ≥ height of driver i." That is, from driver i's perspective, looking forward (to the right, assuming positions increase to the front?), we consider drivers ahead; he can see a driver if that driver is not blocked by a driver of height >= his own height. Typically in such problems, one can see over drivers that are shorter than oneself, but cannot see past the first driver with height >= own height. So the number of drivers he can see in front is the count of drivers that are visible before the first driver with height >= H[i] (including that driver? Let's parse: "a driver i cannot see beyond another driver j if height of j ≥ height of driver i." So if there is a driver j ahead such that H[j] >= H[i], then i cannot see any driver beyond j (i.e., behind j from i's perspective). But can he see j? The wording: "cannot see beyond another driver j" implies he cannot see past j, but does he see j? Typically, you can see the driver that blocks you. Usually problems like "number of buildings you can see" count the building that blocks you as visible. But careful: "he can see in front of him" includes the drivers that are in front that are not blocked by any intermediate driver. If a driver j has height >= H[i], then i cannot see beyond j, but he may still see j if j is the immediate next driver? Let's think: The line of sight is straight? Drivers are moving in a straight line, so consider i looking forward. He sees drivers in order. He will see driver i+1 if it's in front (obviously). For driver i+2, if i+1's height is less than H[i], i might see over i+1 and see i+2. However, if i+1's height is >= H[i], then i cannot see beyond i+1, meaning he cannot see i+2, i+3, ... So the visible drivers are those up to and including the first driver with height >= H[i]? Actually, does he see that blocking driver? The phrase "cannot see beyond another driver j" suggests that j is seen, but beyond j is not. So j is visible. So the set of visible drivers in front is: starting from the immediate next driver (i+1) up to and including the first driver j (j>i) such that H[j] >= H[i]. If there is no such driver, then he can see all drivers from i+1 to n.
Similarly, looking behind (to the left), the same rule: he can see drivers behind him until (and including) the first driver to the left with height >= H[i]; if none, then all drivers from 1 to i-1.
Thus, for each i, X_i = (next_greater_or_equal_on_right_index - i - 1? Wait, careful: The number of drivers he can see in front = number of drivers from i+1 up to the first j (j>i) with H[j] >= H[i], inclusive. So the count = (j - i) if such j exists, else (n - i). Similarly, behind: find the first k < i such that H[k] >= H[i], then number of drivers he can see behind = (i - k) if such k exists, else (i - 1). Actually if k is the first from left that is >=, then drivers from k to i-1 are visible? But careful: when looking behind, the order is from i-1 downwards. The first driver that blocks is the nearest driver to the left with height >= H[i]. Then i can see all drivers from i-1 down to that blocker inclusive? If the blocker is at index k, then i can see drivers k, k+1, ..., i-1? But k is the blocker, so yes he sees k, and cannot see beyond k (further left). So visible count = i - k. If no such k, then visible count = i - 1 (since all from 1 to i-1 are visible). So total X_i = (count_front) + (count_behind).
We need to compute for each i the sum, then compute SIGHT = (X_i * i) mod MOD, then find the maximum SIGHT. In case of ties, choose smallest index.
Now constraints: n up to 1e5 per test, t up to 50, but total across tests may be up to? Not specified, but typical total sum of n maybe up to 1e5? However t up to 50, n each up to 1e5, so worst-case total 5e6, which is high but maybe acceptable with O(n) or O(n log n) per test. Need efficient.
We can compute for each position, the index of the next greater-or-equal element to the right, and the next greater-or-equal element to the left. That's classic monotonic stack problem. For "next greater or equal" (including equal), we need first element on the right that has height >= current. Similarly on left. Typically "next greater element" is strict >, but here it's >=. So we need to treat equality as blocking. Because if heights are equal, can he see beyond? The rule: "cannot see beyond another driver j if height of j ≥ height of driver i". So if equal, then j blocks. So yes, >=.
Thus we can compute arrays: R[i] = index of the first j > i such that H[j] >= H[i], or n+1 if none. Similarly L[i] = index of the first k < i such that H[k] >= H[i], or 0 if none.
Then count_front = if R[i] == n+1 then n - i else R[i] - i. But careful: Do we include the blocking driver? Yes, visible includes that driver? Let's re-evaluate with sample.
Sample Input 1:
2 test cases.
First test case: n=5, H = [4,1,2,1,4]
Let's compute manually:
Indices:1:4, 2:1, 3:2, 4:1, 5:4
Compute X_i.
Driver 1 (height 4): Look front: Starting from 2, find first j>=4. Driver2=1<4, driver3=2<4, driver4=1<4, driver5=4>=4 (so j=5). So visible front = drivers 2,3,4,5? That's 4 drivers (2-5). Count = 5-1? Actually from index2 to 5 inclusive = 4 drivers. So front count = 4? But we might also consider behind: driver1 has no behind, so count_behind=0. So X1=4. SIGHT=4*1=4.
Driver2 (height 1): Front: next >=1? All heights are >=1? Actually height1=4 is >=1 but it's behind? Wait front means indices >2. Starting from 3: H3=2>=1 (first >=1). So visible front includes driver3? Yes, since it's the first >=. So front count = (3-2)=1? Actually includes driver3 only. But also driver4? Can he see driver4? No, because driver3 (height2) is >=1, so blocks beyond. So he cannot see driver4 or 5. So front count=1 (only driver3). Behind: first driver left with >=1? Driver1=4>=1, so behind count = 2-1 =1 (only driver1). So X2=2. SIGHT=2*2=4.
Driver3 (height2): Front: first >=2 to right? Drivers: index4=1<2, index5=4>=2. So visible front includes driver4? driver4=1 <2, so he can see over driver4? Actually driver4 is shorter, so he can see over it and see driver5? Wait the rule: cannot see beyond a driver j with Hj >= Hi. So to see driver5, we need to check if any driver between 3 and 5 has H>=2. Index4 has H=1 <2, so it does not block. So driver5 is visible? But driver5 has H=4>=2, and it is not between; it's the target. The rule says i cannot see beyond another driver j if Hj >= Hi. So for driver5, is it "beyond"? Driver5 is at the end; to see driver5, we must check drivers in between. Only driver4 is between. driver4's height=1<2, so it doesn't block. So driver5 is visible. However, driver5 itself is a driver with height>=2. Since it's the first driver with height>=2 encountered, does it block? The phrase "cannot see beyond another driver j" implies that when you encounter j (which is >=), you cannot see beyond j, but you see j itself. So yes, driver5 is visible. So visible front drivers: index4 (1) and index5 (4). So front count = 2? Actually index4 and index5 are both visible. But careful: He can see driver4 because it's directly in front? Actually driver3 can see driver4 (since it's next, and H4<H3). Then driver5 is also visible because driver4 is shorter, so not blocking. So visible front count = number of drivers from i+1 to the first j>=Hi inclusive? That includes all drivers from i+1 up to j? Let's see: In this scenario, the first j with Hj>=2 is index5. According to our rule, visible front includes drivers i+1,...,j. That would be indices 4 and 5, so count = 2 = 5-3 = 2. So our formula: if R[i] exists, visible front count = R[i] - i. That yields 5-3=2. Good.
If R[i] does not exist, visible front count = n - i (since i+1..n all visible). So formula works.
Now behind for driver3: find first left >=2. Left side: index2=1<2, index1=4>=2. So first left >=2 is index1. So visible behind drivers: indices 2 and 1? According to rule: drivers from i-1 down to that blocker inclusive. So indices 2 and 1, that's two drivers? But driver2 height1 is <2, driver1 height4 is >=2. So visible behind = 2? That is i - L[i] = 3-1=2. So X3 = front(2) + behind(2)=4. SIGHT=4*3=12.
Driver4 (height1): Front: first >=1 to right? index5=4>=1, so front count = 5-4=1 (only driver5). Behind: first left >=1? index3=2>=1? Actually index3 height2>=1, so L=3? Wait index3 is at pos3, with H=2>=1, and it's the nearest left? Check indices: 4-1=3 (height2) is >=1, so L[4]=3. But also index2=1 is <1? Actually height1 =1, equal is >=, so index2=1 is also >=1? H2=1, so yes, 1>=1. But we need the first driver to the left that is >=, meaning the closest. For i=4, left indices: 3 (H=2) is >=1, so it's the first one encountered. So L[4]=3. Then behind count = i - L[4] = 4-3=1 (only driver3). So X4=1+1=2. SIGHT=2*4=8.
Driver5 (height4): Front: none, front count=0. Behind: first left >=4? Check left indices: index4=1<4, index3=2<4, index2=1<4, index1=4>=4. So L=1. Behind count = 5-1=4. X5=4. SIGHT=4*5=20.
So SIGHT values: 4,4,12,8,20. Max is 20 for driver5. Sample output says 5. So matches.
i=1: H=5. Right: all <5? Actually 2:1,3:2,4:4<5? 4<5, so <, 5:1<5. So no >=5 to right. So R1=n+1=6. Front count = n-i = 4? Actually n=5, so front=4. Behind=0. X1=4. SIGHT=4*1=4.
i=2: H=1. Right: first >=1? i+1=3:2>=1, so R2=3. Front=3-2=1 (only driver3). Behind: left first >=1? i-1=1:5>=1, so L2=1. Behind=2-1=1. X2=2. SIGHT=2*2=4.
i=3: H=2. Right: first >=2? index4:4>=2, so R3=4. Front=4-3=1 (only driver4). Behind: left first >=2? index2:1<2, index1:5>=2, so L3=1. Behind=3-1=2 (drivers 2 and 1). X3=1+2=3. SIGHT=3*3=9.
i=4: H=4. Right: first >=4? index5:1<4, so none, R4=6? Actually no >=4, so R4=6. Front=5-4=1 (only driver5). Behind: left first >=4? index3:2<4, index2:1<4, index1:5>=4, so L4=1. Behind=4-1=3 (drivers 3,2,1). X4=1+3=4. SIGHT=4*4=16.
i=5: H=1. Right: none, front=0. Behind: first left >=1? index4:4>=1, so L5=4. Behind=5-4=1 (driver4). X5=1. SIGHT=1*5=5.
SIGHT: 4,4,9,16,5. Max is 16 for driver4. Sample output says 4. So matches.
Thus interpretation is correct.
Now we need to compute for each i, L[i] = index of previous greater-or-equal element, and R[i] = index of next greater-or-equal element.
We can compute using monotonic stack in O(n). Usually for previous greater (or equal) we iterate from left to right, maintaining stack of decreasing heights (strictly? Actually we need first >= to left. Standard algorithm for previous greater element (strictly greater) uses a stack that maintains decreasing heights. For >=, we need to treat equality as blocking, so we want the first element that is >=. So if we have equal heights, it should be considered as greater-or-equal, meaning that when we encounter a height that equals the current, it will be considered as a blocker. For previous greater-or-equal, we want the closest index on left with H >= current. Using a stack that maintains indices with increasing heights? Let's derive.
Standard method for "next greater element" (strict greater) uses a monotonic decreasing stack (heights decreasing from bottom to top). For "next greater or equal", we need to stop at equal as well. So we need to adjust the condition when popping. Typically, when we want next greater (strict), we pop while stack not empty and H[stack.top] <= H[i]? Actually if we want next greater, we are finding for each element the first element to the right that is greater. The typical algorithm processes from right to left, maintaining stack of indices with increasing heights? Wait there are two variants: processing left to right to find next greater for each: While stack not empty and H[stack.top] < H[i], then i is the next greater for stack.top, pop. Then push i. That yields strict greater because we pop when H[stack.top] < H[i]. For >=, we would pop while H[stack.top] <= H[i] because if equal, then i is also >= and thus is the next greater-or-equal for stack.top. But careful about order: If we have equal heights, the first equal to the right should be considered as the blocker. In this algorithm, if we pop when <=, then the first equal will be assigned as next greater-or-equal, and later equal heights further right will not be considered because earlier ones are popped. That works.
But for previous greater-or-equal, we can process from left to right: maintain stack of indices with decreasing heights? Let's think: For previous greater (strict), we can process from left to right, and for each i, while stack not empty and H[stack.top] <= H[i], pop? Actually we want the nearest left element with H >= current. So if we maintain a stack that keeps elements in decreasing order of heights, then the top of stack will be the previous greater or equal? Let's illustrate: As we move left to right, we want for each i, the most recent index j < i such that H[j] >= H[i]. If we maintain a stack where heights are strictly decreasing, then for each new i, we can pop from stack while H[stack.top] < H[i] (since if top is less than current, then it cannot be a >= for future elements? Actually we are finding previous >= for current i. The stack should hold candidates that could be previous >= for future indices. For previous greater (strict >), we usually maintain stack of decreasing values (so that top is the most recent element greater than current). The algorithm: iterate i from left to right: while stack not empty and H[stack.top] <= H[i], pop; then if stack empty, no previous greater; else previous greater is stack.top; then push i. That yields previous greater (strict) because we pop elements that are <= current, so the remaining top is > current. For previous greater-or-equal, we need to pop while H[stack.top] < H[i] (strictly less) because we want to keep elements that are equal (since equal is >=). So we pop while top < current. Then after popping, if stack not empty, the top is the first element to left with height >= current (since we've removed all smaller). This also ensures that if there is an equal height before, it will be kept (since not popped because condition is <). So correct.
For next greater-or-equal, we can process from right to left with analogous logic: while stack not empty and H[stack.top] < H[i] pop; then R[i] = stack.empty()? n+1 : stack.top; then push i. (because we pop those smaller to find first >= to right). That yields next >=.
Alternatively, we can compute both using one pass? But separate passes are fine.
Thus we compute L[i] and R[i] in O(n).
Then for each i, compute front_count = (R[i] - i) if R[i] <= n else (n - i)?? Wait careful: If R[i] = index of first >= to right, then visible front includes i+1 ... R[i] inclusive. So count = R[i] - i. If no such (R[i]=n+1), then count = n - i (since i+1...n inclusive). That's also equal to (n+1 - i) - 1? Actually n - i = (n+1 - i) - 1? But anyway, we can compute as front = (R[i] == n+1) ? (n - i) : (R[i] - i). But note: R[i] - i gives the number of drivers from i+1 to R[i] inclusive? For example, i=3, R[3]=5, then 5-3=2, which matches indices 4 and 5. Yes because difference in indices count = number of steps to go from i to R, excluding i but including R. So formula works.
Similarly, behind count = (i - L[i]) if L[i] > 0 else (i - 1). But if L[i]==0, means no left >=, then all left drivers visible: count = i - 1. So behind = (L[i] == 0) ? (i - 1) : (i - L[i]).
But careful: if L[i]==0, then i-0 = i, which would be too many (includes i itself?), so we need special case. So behind = i - L[i] if L[i] !=0 else i-1.
Alternatively, we could set L[i] to 0 to indicate none, and then compute behind = i - L[i] - (L[i]==0 ? 1 : 0)? But easier: if L[i]==0 then behind=i-1 else behind=i-L[i].
Now X_i = front + behind.
Then S_i = (X_i * i) % MOD. We need to find index i with maximum S_i; ties break by minimum index.
Now complexity: O(n) per test. Total sum n across tests up to? t up to 50, each up to 1e5, worst-case 5e6, O(5e6) is fine.
Potential pitfalls: MOD = 1e9+7, but we only need to compute S_i mod MOD. However, X_i can be up to n-1 (front max n-i, behind max i-1, sum up to n-1). Actually max X_i could be (n-1) if driver sees all others. For i in middle, it's at most n-1. So X_i up to 1e5, i up to 1e5, product up to 1e10, fits in 64-bit. We can compute using long long, then mod. Or use Python big ints.
We'll implement in Python given the problem statement presumably in any language? But we are to provide a solution explanation? The user is ChatGPT, but likely they want a solution in a programming language, maybe Python. The instruction says "As an AI, I can provide a solution." Usually we output code. So we'll produce a Python solution.
But need to be careful about stack operations for large n, Python recursion? Not needed.
Let's design the algorithm:
For each test case:
Read n.
Read list H (0-index or 1-index). We'll use 0-index for internal but we need positions 1-indexed for output. We'll store H as list of ints length n.
Compute arrays L (size n) and R (size n). Use stack.
Implementation details:
Compute L:
Initialize stack empty list.
For i in range(n):
while stack and H[stack[-1]] < H[i]: # pop while top's height < current height
stack.pop()
if stack:
L[i] = stack[-1] # index of previous >= (0-index)
else:
L[i] = -1 # indicating none (we'll later convert to 0 for 1-index)
stack.append(i)
But note: This gives the index of the previous element with height >= current (0-index). We'll later convert to 1-index for calculations? For computing behind count, we can use 1-index: position = i+1. So we can keep arrays in 1-index to simplify. Let's do everything 1-indexed. We'll make H of size n+2 with dummy at index 0 maybe. But Python list 0-index is fine, we just adjust formulas.
Let's decide to work with 0-index but compute behind using i+1 (position). We'll compute L_1index = previous >= index in 1-index, or 0 if none. So we can compute L0 array (0-index) with values -1 if none. Then for position pos = i+1, L_1 = L0[i] + 1 if L0[i] != -1 else 0.
Similarly for R: compute next >= index in 0-index, or n if none (i.e., no such index). We'll set R0[i] = index (0-index) of next >=, or n if none. Then for position pos, R_1 = R0[i] + 1 if R0[i] != n else n+1.
But we need to compute front count: if R0[i] == n then no such, front = n - (i+1)? Actually pos = i+1. n - pos = n - (i+1). If R0[i] is index j (0-index), then front = (j - i) because number of drivers from i+1 to j inclusive = j - i (since j - i gives count). For example, i=2 (0-index) corresponds to pos=3, j=4 (0-index) corresponds to pos=5, front = 5-3? Actually 5-3=2. j-i = 4-2=2. Yes. So we can compute front directly with 0-index: front = (R0[i] - i) if R0[i] != n else (n - i - 1? Wait careful: If R0[i] == n (meaning no >= to right), then visible front includes all drivers from i+1 to n-1 (0-index). Count = (n-1) - (i) + 1? Actually n-1 is last index. Number of indices from i+1 to n-1 inclusive = (n-1) - (i+1) + 1 = n - i - 1. Let's test: n=5, i=4 (0-index last), front count should be 0. n - i - 1 = 5-4-1=0. Good. i=3 (pos4), front = if no >=, then drivers index4 only (since i=3, i+1=4, n-1=4). Count=1. n - i - 1 = 5-3-1=1. So formula: front = n - i - 1 if R0[i] == n else R0[i] - i.
Alternatively, we can compute with 1-index uniformly after conversion.
I'll use 1-index for clarity: Let's create arrays H[1..n] (use list with index 0 dummy). Then compute L[1..n] and R[1..n] as described.
Implementing monotonic stack with 1-index:
For L (previous greater-or-equal):
Initialize empty stack (stores indices). Loop i from 1 to n:
while stack not empty and H[stack[-1]] < H[i]: # pop while top's height < H[i]
stack.pop()
if stack: L[i] = stack[-1]
else: L[i] = 0 # 0 indicates none
stack.append(i)
For R (next greater-or-equal):
Initialize empty stack. Loop i from n down to 1:
while stack not empty and H[stack[-1]] < H[i]: # pop while top's height < H[i]
stack.pop()
if stack: R[i] = stack[-1]
else: R[i] = n+1
stack.append(i)
Double-check: For R, we process from right, maintaining stack of indices with decreasing? Actually we need next >= to right. The typical algorithm: iterate from n to 1, and while stack not empty and H[stack[-1]] < H[i] pop; then if stack empty, no next >=, set to n+1; else R[i] = stack[-1]; then push i. This works because stack maintains indices with increasing heights? Let's test: Suppose H = [4,1,2,1,4] (1-index). For i=5 (H=4): stack empty => R[5]=6. push 5. i=4 (H=1): while top (index5, H=4) < 1? 4<1 false, so don't pop. stack not empty => R[4]=5. push 4. stack now has [5,4] (indices). But note heights: H[5]=4, H[4]=1; stack from bottom to top: 5(4),4(1) -> decreasing? Actually 4 is less than 4? It's decreasing? Not exactly. The invariant: stack should have increasing heights from bottom to top? Let's analyze: In this algorithm, we pop while H[stack[-1]] < H[i]. So after processing i, we push i. This ensures that the stack after each step is monotonic decreasing? Actually consider we are moving from right to left. We want to keep indices of elements that could be next greater-or-equal for future (left) elements. When we encounter a new element i, we pop all indices that have height less than H[i], because those indices cannot be the next greater-or-equal for any element left of i (since i is closer and has height >= those). So after popping, the top of stack (if any) is the first element to the right with height >= H[i]. Then we push i. This results in stack that is decreasing in height from bottom to top? Let's simulate: Start empty. i=5, H=4: push -> stack [5].
i=4, H=1: condition while stack top H[5]=4 < 1? false, so no pop. So R[4]=5. push 4 -> stack [5,4] (heights 4,1). So stack heights are 4 (bottom), then 1 (top). That's decreasing? Actually 4 > 1, so it's decreasing (higher at bottom, lower on top). That is typical for monotonic decreasing stack when iterating from right. Good.
i=3, H=2: while stack top H[4]=1 < 2? true, pop 4. Now stack top is 5, H[5]=4 < 2? false. So R[3]=5. push 3 -> stack [5,3] (heights 4,2). Now heights: 4 > 2, decreasing.
i=2, H=1: while top H[3]=2 < 1? false. R[2]=3. push 2 -> stack [5,3,2] (heights 4,2,1) decreasing.
i=1, H=4: while top H[2]=1 < 4? true, pop 2; top now H[3]=2 < 4? true, pop 3; top now H[5]=4 < 4? false (since 4 < 4? false, because condition is <, not <=). So stop. R[1]=5 (since stack not empty). push 1 -> stack [5,1] (heights 4,4). Now heights: 4 == 4, so not strictly decreasing but non-increasing (since equal allowed). Actually after pushing 1, stack becomes [5,1] with heights 4 and 4, which is non-increasing (4 >= 4). That's fine because we popped only when <, so equal heights remain. This yields R[1]=5, which is correct? For driver1 (height4), first >= to right is driver5 with height4, but there is also driver? driver2,3,4 are all less, so first >= is driver5. So correct.
Now check case with equal heights earlier: Suppose H = [2,2,1]. For driver1 (height2), next >= to right should be driver2 (height2). Let's simulate: n=3.
i=3 H=1: R[3]=4, stack [3]
i=2 H=2: while top H[3]=1 < 2? true, pop 3. stack empty => R[2]=4? Wait that would be incorrect. Let's do carefully: Starting from right.
Initialize stack empty.
i=3: H=1, stack empty => R[3]=4. push 3.
i=2: H=2, while stack not empty and H[stack[-1]] < H[2]? H[3]=1 < 2 true => pop 3. Now stack empty => R[2] = 4? But we know there is driver3 with height1 <2, but no >= to right? Actually driver2 is at index2, right side has index3 with height1 which is not >=2. So indeed there is no driver with height >=2 to the right. So R[2] should be 4. That is correct. Because driver2 cannot see beyond any >=? Actually he can see driver3? Yes driver3 height1 <2, so visible, but that's not a blocker. There is no >= to right, so visible front includes all to the right. So R[2]=4 is correct.
Now i=1: H=2. stack currently? After i=2 we pushed 2. So stack = [2]. For i=1: while H[2]=2 < H[1]=2? false (since not <). So R[1]=2 (stack top). Then push 1, stack becomes [2,1]? Actually after pushing 1, stack is [2,1] (since we push at end). But this violates decreasing? heights 2 and 2 are equal. That's fine. So R[1]=2, correct.
Thus algorithm works.
Now compute behind count using L[i]: if L[i]==0 then behind = i-1 else behind = i - L[i].
Front count: if R[i]==n+1 then front = n - i else front = R[i] - i.
X_i = front + behind.
S_i = (X_i * i) % MOD.
Now find max.
Edge Cases: n=1. Then for driver1: behind = 0, front = 0, X=0, S=0. Winner is driver1 (since only one). Should output 1. Our formulas: L[1]=0, behind = i-1=0; R[1]=n+1=2, front = n-i=0; X=0. Good.
Equal heights: If many equal, the nearest one blocks. E.g., H = [3,3,3]. Driver1: L=0, behind=0; R=2 (since first >= to right is driver2), front=2-1=1 (only driver2? But also driver3? Let's see: driver1 sees driver2 and driver3? He cannot see beyond driver2 if driver2 is >=3 (equal). So driver1 sees driver2, but not driver3 because driver2 blocks. So front=1. So X=1. Driver2: L=1 (prev >=), behind=2-1=1 (driver1), R=3, front=3-2=1 (driver3). So X=2. Driver3: L=2, behind=3-2=1 (driver2), R=4, front=0, X=1. That seems plausible. Check manually: driver2 sees driver1 (behind) and driver3 (front) because both are equal? Actually driver2 looks left: first left >=3 is driver1, so sees driver1 only. Right: first right >=3 is driver3, sees driver3 only. So X=2. Good.
Now what about if there is a taller driver further but a shorter driver in between? For example H=[1,5,2]. Driver1 (1): front: first >=1? driver2=5>=1, so front=2-1=1 (only driver2). So cannot see driver3? Because driver2 blocks. So X=1. driver2 (5): front: first >=5? none, front=2 (drivers 3? Actually front count = n - i = 3-2=1? Wait n=3, i=2: front = 3-2=1 (only driver3). behind: first left >=5? none, behind=i-1=1 (only driver1). So X=2. driver3 (2): behind: first left >=2? driver2=5>=2, so behind=3-2=1 (driver2 only). So X=1. That's correct.
All good.
Now implement.
Complexities: O(n) per test.
Now need to handle multiple test cases. Since total n could be up to 5e6, we need efficient input parsing. Use sys.stdin.read() to read all and split.
Potential pitfalls: H[i] can be 0? Yes, 0 <= H[i] <= 1e6. 0 heights are allowed. Then >= includes 0. So drivers with height 0 can see only up to the first driver with height >=0, i.e., any driver (since all heights >=0). So essentially a driver with height 0 can see only the immediate next driver? Actually because all drivers have height >=0, the first driver to the right will have height >=0, so visible front count = 1 (the immediate next). Similarly behind = 1 if there is a driver left. But if there is a driver with height 0 and others also 0, then the nearest equal or greater (>=0) will be the immediate neighbor (since equal qualifies). So that's fine.
Now modulo: Since S_i computed as (X_i * i) % MOD. But we need to compare S_i for maximum; we can compute S_i mod MOD, but the max comparison should be based on the modulo value? The problem says "displaying the index of the winning driver." SIGHT is defined as (X * P) % 1000000007. So the SIGHT is the remainder modulo MOD. So we compare these remainders. In case of tie (same remainder), choose smallest index. So we compute value = (X * i) % MOD. Then find max. Since product < 1e10, we can compute mod.
But careful: If MOD is large, product is < 1e10, mod may be less than product, but we compare remainders.
Now we must output the driver index (1-indexed) for each test case.
Now test with sample.
Let's write pseudocode:
def solve():
import sys
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
MOD = 1000000007
out_lines = []
for _ in range(t):
n = int(next(it))
H = [0] + [int(next(it)) for __ in range(n)] # 1-indexed
# Compute L
L = [0](n+1)
stack = []
for i in range(1, n+1):
while stack and H[stack[-1]] < H[i]:
stack.pop()
if stack:
L[i] = stack[-1]
else:
L[i] = 0
stack.append(i)
# Compute R
R = [0](n+1)
stack = []
for i in range(n, 0, -1):
while stack and H[stack[-1]] < H[i]:
stack.pop()
if stack:
R[i] = stack[-1]
else:
R[i] = n+1
stack.append(i)
best_idx = 1
best_sight = -1
for i in range(1, n+1):
behind = (i - L[i]) if L[i] != 0 else (i-1)
front = (R[i] - i) if R[i] != n+1 else (n - i)
X = behind + front
sight = (X * i) % MOD
if sight > best_sight or (sight == best_sight and i < best_idx):
best_sight = sight
best_idx = i
out_lines.append(str(best_idx))
sys.stdout.write("\n".join(out_lines))
We need to check front formula: if R[i] != n+1: front = R[i] - i. Test with i=5 (n=5, R[5]=n+1): front = n - i = 5-5=0, correct. i=1, R[1]=5: front=5-1=4, correct. i=3, R[3]=5: front=5-3=2, correct.
Case1: n=5, H=[4,1,2,1,4] (1-index). Compute L and R as above? Let's simulate quickly: L: i=1: stack empty, L[1]=0; push1. i=2: while stack top H[1]=4 < 1? false; L[2]=1; push2 => stack[1,2]. i=3: while top H[2]=1 < 2? true pop2; now top H[1]=4 <2? false; L[3]=1; push3 => stack[1,3]. i=4: while top H[3]=2 <1? false; L[4]=3; push4 => stack[1,3,4]. i=5: while top H[4]=1 <4? true pop4; top H[3]=2 <4? true pop3; top H[1]=4 <4? false; L[5]=1; push5 => stack[1,5]. So L=[0,0,1,1,3,1] (L[1]=0, L[2]=1, L[3]=1, L[4]=3, L[5]=1) matches earlier? Earlier we had L[4]=3, L[5]=1, L[2]=1, L[3]=1. Yes.
R: iterate from 5 to 1.
i=5: stack empty, R[5]=6; push5.
i=4: while top H[5]=4 <1? false; R[4]=5; push4 => stack[5,4].
i=3: while top H[4]=1 <2? true pop4; now top H[5]=4 <2? false; R[3]=5; push3 => stack[5,3].
i=2: while top H[3]=2 <1? false; R[2]=3; push2 => stack[5,3,2].
i=1: while top H[2]=1 <4? true pop2; top H[3]=2 <4? true pop3; top H[5]=4 <4? false; R[1]=5; push1 => stack[5,1]. So R=[0,5,3,5,5,6] (R[1]=5,R[2]=3,R[3]=5,R[4]=5,R[5]=6). Good.
Now compute sights as earlier and max=5. Good.
Second sample: H=[5,1,2,4,1]. L: i1: L1=0; push1. i2: while H1=5<1? false, L2=1; push2. i3: while H2=1<2? true pop2; now H1=5<2? false; L3=1; push3. i4: while H3=2<4? true pop3; H1=5<4? false; L4=1; push4. i5: while H4=4<1? false; L5=4; push5. So L = [0,0,1,1,1,4] (L[1]=0,L[2]=1,L[3]=1,L[4]=1,L[5]=4). R: i5: R5=6; push5. i4: while H5=1<4? false? Actually H5=1<4 true? Condition is <, so 1<4 true, pop5? Wait careful: for R, we pop while stack top height < H[i]. So i=4, H[4]=4, stack has [5] with H[5]=1, 1<4 true, pop5. stack empty => R[4]=6? But earlier we had R[4]=5? Let's recalc manually: For driver4 (height4), the first >= to right is driver5? Driver5 height1 is not >=4, so there is no >= to right. So R[4] should be n+1=6. But earlier I computed front for driver4 as 1 (only driver5) because I considered no >= and thus front = n - i = 5-4=1. That's correct if there is no >=. So R[4] should be 6, not 5. My earlier manual R[4] for sample2 mistakenly said R[4]=5? Actually earlier I computed: i=4: R4=6? I wrote "R4=6? Actually no >=4, so R4=6". But later I computed front count as 1 because n-i=1. So that's consistent: R[4]=6. So in my algorithm, R[4] will be 6. Let's recalc R properly:
n=5.
i=5: stack empty, R5=6, push5.
i=4: H4=4. while stack and H[stack[-1]] < 4? stack top index5 H5=1 <4 true -> pop5. stack empty -> R4=6. push4 -> stack[4].
i=3: H3=2. while stack top H4=4 <2? false. R3=4. push3 -> stack[4,3].
i=2: H2=1. while top H3=2 <1? false. R2=3. push2 -> stack[4,3,2].
i=1: H1=5. while top H2=1 <5 true -> pop2; top H3=2<5 true -> pop3; top H4=4<5 true -> pop4; stack empty -> R1=6. push1 -> stack[1].
So R = [0,6,3,4,6,6]? Actually R[1]=6, R[2]=3, R[3]=4, R[4]=6, R[5]=6. Check: For driver2 (height1), next >=1 is driver3 (height2) so R2=3 correct. For driver3 (height2), next >=2 is driver4 (height4) so R3=4 correct. For driver4, none =>6. For driver5, none =>6. For driver1, none? Actually driver1 height5, no >=5 to right, so R1=6 correct.
Now edge case: When there are equal heights, the condition uses < for popping, so equal heights remain, causing the nearest equal to be considered as previous/next >=. That's correct.
Now complexity: O(n) each test.
Potential issues: Memory: arrays of size n+1 fine.
Now we should consider the possibility that n is 0? Constraints say 1 ≤ n, so n>=1.
Now I'll produce final solution code.
But also need to note that t can be 0? Actually constraint says 0 ≤ t ≤ 50. So if t=0, we should output nothing. Our code reading t and then loop range(t) will handle t=0 fine.
Now we can write the solution.
But wait: The problem statement says "X = number of drivers he can see in front of him + number of drivers he can see behind him". Is there any nuance about the direction? The line is straight, but front/back are relative to direction of motion? Usually front is ahead, behind is behind. So we assume positions increasing to front. So our interpretation stands.
Now confirm with additional tests:
Test 1: n=1 => output 1.
Test 2: all same height, n=3 => compute S_i: i=1: behind 0, front 1 => X=1, sight=1; i=2: behind=1, front=1 => X=2, sight=4; i=3: behind=1, front=0 => X=1, sight=3; max sight is 4 at i=2, output 2.
Test 3: decreasing heights: H=[5,4,3,2,1]. Then each driver sees all drivers to the right? For i=1 (5), any j with height >=5? Only itself, none to right, so front = n-i =4. behind 0. X=4, sight=4. i=2 (4): left: first >=4 is driver1 (5), behind =1; right: first >=4? none (since all smaller), front = 3; X=4, sight=8. i=3: behind: first >=3? driver1? Actually driver2=4>=3, so nearest left >= is index2, behind = 3-2=1; front: first >=3? none, front=2; X=3, sight=9. i=4: behind: nearest left >=? driver3=3>=2? Actually 3>=2, so L=3, behind=4-3=1; front: none? Actually driver5=1 <2, so front=1; X=2, sight=8. i=5: behind: nearest left >=1? driver4=2>=1, so L=4, behind=1; front=0; X=1, sight=5. Max sight=9 at i=3. That seems plausible.
Test 4: increasing heights: H=[1,2,3,4,5]. i=1: front: first >=1 is driver2 (2) -> front=1; behind=0 => X=1, sight=1. i=2: behind: first >=2? driver1=1<2, so none => behind=1; front: first >=2? driver3=3 -> front=1; X=2, sight=4. i=3: behind: first >=3? driver2=2<3, none => behind=2; front: first >=3? driver4=4 -> front=1; X=3, sight=9. i=4: behind: first >=4? driver3=3<4, none => behind=3; front: first >=4? driver5=5 -> front=1; X=4, sight=16. i=5: behind: first >=5? none => behind=4; front=0; X=4, sight=20. Max 20 at i=5. That matches.
Now we need to ensure that modulo operation does not affect comparison: Since we take mod, and we compare integers after mod, it's fine.
Now output the driver index.
Now we should think about potential pitfalls in stack condition: Use < (strictly less) for popping to keep >= elements. This works for both L and R.
But for L we popped while H[stack[-1]] < H[i]. That's correct because we want to remove smaller ones, leaving the nearest with height >=.
For R we also pop while H[stack[-1]] < H[i].
Now confirm for equal heights: Example H=[2,2]. L: i=1: L1=0; push1. i=2: while H1=2 < 2? false; so L2=1; correct. R: i=2: R2=3; push2. i=1: while H2=2 < 2? false; R1=2; correct. So front for i=1: R1=2 => front=1 (driver2). behind: L1=0 => behind=0. So X=1. For i=2: behind: L2=1 => behind=1; front: R2=3 => n-i=0; X=1. So both have X=1.
Now scenario where there are multiple equal heights: H=[2,1,2]. Let's compute. i=1 (2): L1=0, behind=0; R1? next >=2? index3=2, so R1=3, front=2? Wait front = R1 - i = 3-1=2 (drivers 2 and 3). But can driver1 see driver2 and driver3? Driver2 height1 <2, so not blocking; driver3 height2 >=2, so driver1 sees driver2 and driver3? According to rule: He can see driver2 because it's directly in front; he can also see driver3 if no blocking driver with height>=2. Since driver2 is shorter, not blocking, and driver3 is >=2, he sees driver3 as well. So yes, front=2. So X=2, sight=2. i=2 (1): L2? previous >=1? driver1=2 >=1, so L2=1, behind=1; R2? next >=1? driver3=2 >=1, so R2=3, front=1 (driver3 only because driver3 is the first >= and included). Actually front count = R2 - i = 3-2=1. So X=2, sight=4. i=3 (2): L3? previous >=2? driver2=1<2, driver1=2>=2, so L3=1, behind=3-1=2 (drivers2 and1? But driver2 is 1<2, but driver1 is >=2, so behind includes driver2 and driver1). Yes behind=2. front: none, R3=4? n=3 so n+1=4, front=0. X=2, sight=6. So max sight 6 at i=3. But check manually: driver3 sees behind: driver2 (1) and driver1 (2). Since driver2 is shorter, he can see over driver2 to driver1? But driver1 is >=2 and blocks? The rule: looking behind, driver3 sees drivers behind until first with height >=2. The nearest driver to the left with height >=2 is driver1. So he sees driver2 (since driver2 is between and shorter) and driver1. So behind count=2. So correct. So algorithm works.
Now we should also consider that H[i] can be 0, and we might have many zeros. For zeros, the first >=0 is the immediate neighbor because any height >=0. So L and R will be the immediate neighbor if it exists, else none. For zero heights, popping condition: while H[stack[-1]] < 0? Since H are >=0, this condition will be false if stack top is >=0, which all are. So stack will retain all indices? Actually if we have H[i]=0, then while H[stack[-1]] < 0, since no heights are <0 (all >=0), condition false. So for L: scanning left to right: for i with H[i]=0, we will not pop anything (since all previous heights >=0 are not <0). So L[i] will be stack top (the most recent previous index) if stack not empty, else 0. That gives the immediate left neighbor (since that neighbor's height is >=0). That's correct as the nearest >=0. So good.
Now for R, similar.
Now we need to ensure that we treat the stack correctly for large n.
Now we also need to compute S_i = (X_i * i) % MOD. Since MOD is prime but not relevant.
Now after computing best_idx, output.
Now time complexity: O(n) per test.
Now we must consider memory: storing H, L, R for each test case. That's fine.
Now one more nuance: The problem statement: "X = number of drivers he can see in front of him + number of drivers he can see behind him". But what about the driver himself? Not counted. So X can be 0 to n-1.
Now everything looks correct.
Now I'll produce final answer.
But wait: Could there be a nuance about seeing drivers with equal height? Does he see them? Yes, as per rule, the blocking driver is seen, but not beyond. So equal qualifies as >=, so he sees that driver. So included.
Now double-check with a scenario: H=[3,1,3]. Driver2 (height1) can see front: first >=1? driver3=3, front count=1 (driver3). behind: first >=1? driver1=3, behind=1 (driver1). So X=2. But can driver2 see both? Since driver1 and driver3 are taller, he can see them, and they block view beyond. So correct.
Now scenario: H=[3,2,1]. Driver1 (3): front: first >=3? none, so front=2 (drivers2 and3). But can driver1 see driver3? Driver2 height2 <3, so not blocking; driver3 height1 <3, so also visible. So front=2, correct. Driver2 (2): front: first >=2? none? Actually driver3=1<2, so no >=, front=1 (driver3). behind: first >=2? driver1=3>=2, behind=1. X=2. But does driver2 see driver3? Yes, since driver3 shorter. So front=1. So X=2. Driver3 (1): behind: first >=1? driver2=2>=1 (nearest) so behind=2 (drivers2 and1? Actually nearest >= is driver2, so behind includes driver2 and driver1? Wait driver2 is height2>=1, and driver1 is further left, but driver2 is the first >= encountered, so driver3 sees driver2 and cannot see beyond driver2. So behind = 3-2=1, only driver2. But we might think he sees driver1 as well because driver2 is shorter? No, driver2 is taller (2>=1), so blocks. So behind=1. front=0. X=1. So algorithm gives behind=1 for driver3, not 2. Let's compute using L: For i=3, find previous >=1: scanning left: index2=2>=1, so L=2, behind = i - L = 3-2=1. Good.
Now does driver3 see driver2? Yes. So X=1. So correct.
Now everything consistent.
Now consider performance: Use sys.stdin.buffer.read() for faster input. We'll implement accordingly.
Now final solution.
But wait, the problem says "index of the driver in a given scenario (index of the driver array is 1-N indexed)". So we already use 1-index.
Now produce final code.
Now we need to ensure we handle modulo correctly: Since S_i may be computed as (X_i % MOD) * i % MOD, but X_i up to 1e5, i up to 1e5, product up to 1e10, which fits in Python int, so mod after multiplication is fine.
Now edge case: multiple test cases, we need to reset arrays each time. Our code inside loop re-initializes.
Now I'll write final answer.
One more test: t=0, input just "0". Our code should output nothing.
Now I think it's good.
Now produce final answer with explanation.
But the request: "As the Formula One Grand Prix was approaching...". The user likely wants the solution. We can provide a description then the code.
Given we are ChatGPT, we can provide an answer in the style of problem solution: explanation then code.
I'll produce final response with solution explanation and then Python code.
But maybe they just want the code? Since it's a coding problem, we can output code. But I'll include brief explanation.
Now finalize. ```python
import sys
MOD = 1000000007
def solve() -> None:
data = sys.stdin.buffer.read().split()
if not data:
return
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it))
# 1-indexed heights
H = [0] + [int(next(it)) for _ in range(n)]
code
# previous greater-or-equal (left side)
L = [0] * (n + 1)
stack = []
for i in range(1, n + 1):
while stack and H[stack[-1]] < H[i]:
stack.pop()
L[i] = stack[-1] if stack else 0
stack.append(i)
# next greater-or-equal (right side)
R = [0] * (n + 1)
stack.clear()
for i in range(n, 0, -1):
while stack and H[stack[-1]] < H[i]:
stack.pop()
R[i] = stack[-1] if stack else n + 1
stack.append(i)
best_idx = 1
best_sight = -1
for i in range(1, n + 1):
# drivers seen behind
if L[i] != 0:
behind = i - L[i]
else:
behind = i - 1
# drivers seen in front
if R[i] != n + 1:
front = R[i] - i
else:
front = n - i
X = behind + front
sight = (X * i) % MOD
if sight > best_sight or (sight == best_sight and i < best_idx):
best_sight = sight
best_idx = i
out.append(str(best_idx))
sys.stdout.write("\n".join(out))