#STK0000076. Phần tử có tần suất lớn hơn tiếp theo (Next Greater Frequency)
Phần tử có tần suất lớn hơn tiếp theo (Next Greater Frequency)
Next Greater Frequency
Source: Baekjoon Online Judge
Version: Phuoc Hung OJ Extended
Problem Statement
Given an array . Let be the number of occurrences of value in the entire array. For every position , find the first to its right such that . If it does not exist, the answer is .
Input
The first line contains an integer . The second line contains integers .
Output
Print values. The -th value is the next element whose global frequency is greater than that of , or if none exists.
Subtasks
- Subtask 1 (30 points): ; all other conditions are unchanged.
- Subtask 2 (70 points): , .
Examples
Input
7
1 1 2 3 4 2 1
Output
-1 -1 1 2 2 1 -1
Explanation
First count occurrences in the entire array:
- ;
- ;
- ;
- .
Now inspect each position:
- The first two values are with frequency , the maximum frequency in the array, so both answers are .
- For the value at position , the following values do not have a greater frequency; the final has frequency , so the answer is .
- For the value at position , the next value also has frequency , while the following has frequency , so the answer is .
- For the value at position , the next has frequency , so the answer is .
- For the value at position , the final has frequency , so the answer is .
- The final has no element to its right, so its answer is .
Thus the output is -1 -1 1 2 2 1 -1.