欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Remove Extra One(思维)

发布时间:2023/12/15 编程问答 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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)/~

总结

以上是生活随笔为你收集整理的Remove Extra One(思维)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。