#STK0000074. Giá trị nhỏ hơn gần nhất (Nearest Smaller Values)
Giá trị nhỏ hơn gần nhất (Nearest Smaller Values)
Nearest Smaller Values
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
Given an array . For every position , find the largest position such that and . If no such position exists, the answer for position is .
Input
The first line contains an integer . The second line contains integers .
Output
Print integers. The -th integer is the nearest position to the left of whose value is smaller than , or if it does not exist.
Subtasks
- Subtask 1 (30 points): ; all other conditions are unchanged.
- Subtask 2 (70 points): , .
Examples
Input
8
2 5 1 4 8 3 2 5
Output
0 1 0 3 4 3 3 7
Explanation
Process the positions from left to right:
- : there is no element to the left, so the answer is .
- : . The nearest smaller value on the left is , so the answer is .
- : . Neither nor is smaller than , so the answer is .
- : . The immediately preceding value is smaller, so the answer is .
- : . Since , the answer is .
- : . The closer values and are not smaller than ; , so the answer is .
- : . The values are not smaller than ; , so the answer is .
- : . The immediately preceding value is smaller, so the answer is .
Hence the required index sequence is 0 1 0 3 4 3 3 7.