/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 233 点
問題文
高橋君は山岳地帯をハイキングしています。目の前には N 個の山が一列に並んでおり、左から順に 1, 2, \ldots, N と番号が付けられています。山 i の標高は A_i です。
高橋君は、山脈の中で「独立峰」と呼べる山を探しています。山 i が「独立峰」であるとは、i 以外のすべての山 j(1 \leq j \leq N, \, j \neq i)に対して A_i > A_j が成り立つことを指します。すなわち、独立峰とは N 個の山の中で標高が狭義に最大である山のことです。
独立峰は高々 1 つしか存在しません。最大の標高を持つ山が複数ある場合、条件は狭義の不等号(>)であるため、それらの山はいずれも独立峰にはなりません。一方、N = 1 の場合は、唯一の山が独立峰となります。
N 個の山の標高が与えられるので、独立峰が存在する場合はその標高を、独立峰が存在しない場合は 0 を出力してください。
制約
- 1 \leq N \leq 10^6
- 1 \leq A_i \leq 10^9
- 入力はすべて整数である。
入力
N A_1 A_2 \ldots A_N
- 1 行目には、山の個数を表す整数 N が与えられる。
- 2 行目には、各山の標高を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
出力
独立峰が存在する場合はその標高を、存在しない場合は 0 を、1 行に出力してください。
入力例 1
5 3 1 5 2 4
出力例 1
5
入力例 2
4 3 5 5 2
出力例 2
0
入力例 3
10 14 7 23 9 42 18 42 5 31 12
出力例 3
0
入力例 4
20 100 250 300 450 123 678 999 234 567 890 111 222 333 444 555 666 777 888 1000000000 999999999
出力例 4
1000000000
入力例 5
1 1
出力例 5
1
Score : 233 pts
Problem Statement
Takahashi is hiking in a mountainous area. In front of him, N mountains are lined up in a row, numbered 1, 2, \ldots, N from left to right. The elevation of mountain i is A_i.
Takahashi is looking for a mountain that can be called an "independent peak" in the mountain range. Mountain i is an "independent peak" if A_i > A_j holds for every other mountain j (1 \leq j \leq N, \, j \neq i). In other words, an independent peak is a mountain whose elevation is strictly the maximum among all N mountains.
There exists at most one independent peak. If multiple mountains share the maximum elevation, none of them qualifies as an independent peak because the condition uses a strict inequality (>). On the other hand, if N = 1, the only mountain is an independent peak.
Given the elevations of N mountains, output the elevation of the independent peak if one exists, or 0 if no independent peak exists.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
N A_1 A_2 \ldots A_N
- The first line contains an integer N, representing the number of mountains.
- The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the elevation of each mountain.
Output
If an independent peak exists, output its elevation; otherwise, output 0, on a single line.
Sample Input 1
5 3 1 5 2 4
Sample Output 1
5
Sample Input 2
4 3 5 5 2
Sample Output 2
0
Sample Input 3
10 14 7 23 9 42 18 42 5 31 12
Sample Output 3
0
Sample Input 4
20 100 250 300 450 123 678 999 234 567 890 111 222 333 444 555 666 777 888 1000000000 999999999
Sample Output 4
1000000000
Sample Input 5
1 1
Sample Output 5
1