博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Remove Extra One(思维)
阅读量:4134 次
发布时间:2019-05-25

本文共 1441 字,大约阅读时间需要 4 分钟。

You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible.

We remind that in a sequence of numbers a1, a2, …, ak the element ai is a record if for every integer j (1 ≤ j < i) the following holds: aj < ai.

Input

The first line contains the only integer n (1 ≤ n ≤ 105) — the length of the permutation.

The second line contains n integers p1, p2, …, pn (1 ≤ pi ≤ n) — the permutation. All the integers are distinct.

Output

Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one.

Examples

Input
1
1
Output
1
Input
5
5 1 2 3 4
Output
5
Note
In the first example the only element can be removed.
今天不适合写题,各种wa。
题意:删掉一个数字,使得这个序列中的record数最多。record序列是这样定义的,如果一个数a[i],它在[1~i]之间是最大的。问这样的数字最多的情况是删除哪个数字之后才有的。
思路:这种题目,一般就是考虑删除一个数字之后,对于整个序列的贡献。如果这个数字是1~i最大的,那么删除她对于这个序列来说就是少了一个。如果说这个数字是第二大的,那么删除最大的数字之后,这个数字就成了最大了,那么删除最大的数字对于整个序列的贡献就是增加了一个。这样我们就可以O(n)的时间复杂度处理出每个删除数字的贡献值,取最优的就可以了。
代码如下:

#include 
using namespace std;const int maxx=1e5+100;int n,x,_max,_max1;int dp[maxx]; int main(){
scanf("%d",&n); for(int i = 0; i < n; i ++) {
scanf("%d",&x); if(x>_max) dp[x]=1,_max1=_max,_max=x;//大于最大值就是最大值,记得更新次大值 else if(x>_max1) dp[_max]--,_max1=x; } int ans = 1; for(int i=1;i<=n;i++) {
if(dp[i]

努力加油a啊,(o)/~

转载地址:http://cytvi.baihongyu.com/

你可能感兴趣的文章