题干:
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.
题目大意:
给定一个长度为n的排列p。从排列中删除一个元素,使“记录”的数量尽可能多。
我们要提醒大家,在a1 a2…, ak中,元素ai是一个记录,当且仅当对于每个整数j (1≤j < i),均需满足aj < ai。
解题报告:
其实只需要统计出删除某一个值后,会增加多少个“记录”即可。
转换主元,去考虑 j ,来维护前面的值,会发现只会对前面第二大的元素产生影响,所以对于每一个j,只会更新前面的一个值,set维护一下即可。
发现其实只用到了前两大的元素,所以可以直接维护最大次大值,不难省掉set这个log。
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
set<PII> ss;
set<PII> ::iterator it,itt;
int n,a[MAX],ok[MAX],ans[MAX];
int main()
{cin>>n;for(int i = 1; i<=n; i++) scanf("%d",a+i);for(int i = 1; i<=n; i++) {it = ss.lower_bound(pm(a[i],-1)),itt=it;if(it == ss.end()) ok[i]=1;else {++itt;if(itt == ss.end()) ans[it->SS]++;}ss.insert(pm(a[i],i));}int mx=-1e7,mi=-1;for(int i = 1; i<=n; i++) {int tmp = ans[i] - ok[i];if(tmp > mx) {mx=tmp;mi = a[i];}else if (tmp == mx) mi = min(mi,a[i]);}printf("%d\n",mi);return 0 ;
}