Remove Extra One(思维)

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 <bits/stdc++.h>
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]<dp[ans]) ans=i;
	}
	printf("%d\n",ans);
    return 0;
}
/*7
1 6 7 4 2 5 3*/

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

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值