Remove Extra One
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a permutation pp of length . Remove one element from permutation to make the number of records the maximum possible.
We remind that in a sequence of numbers a1, a2, ..., aka1, a2, ..., ak the element aiai is a record if for every integer j(1 ≤ j < i)j(1 ≤ j < i) the following holds: aj < aiaj < ai.
input
The first line contains the only integer n(1 ≤ n ≤ 105)n(1 ≤ n ≤ 105) — the length of the permutation.
The second line contains nn integers (1 ≤ pi ≤ n)(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.
解题思路
先给一波官方题解:
In this problem you have to find an element after which removal the number of records is maximum possible.
Let ri be an array consisting of 0 and 1 depending on whether the i−thi−th element was a record initially or not. We can compute it easily in O(N)O(N).
Let xixi be the difference between the number of records after removal the i-th element and initial number of records.
Let’s think of how does removal of ai influence the array riri. First of all, riri becomes 0. rj(j < i)rj(j < i) do not change in this case. Some of rj(j > i)rj(j > i), change from 0 to 1. These elements are not records initially, but become records after the removal. These elements are elements which have only one greater element in front of them — aiai.
Here follows an O(n2)O(n2) solution. Let’s fix aiai — the element we are going to remove. Let xi= − rixi= − ri + the number of such jj that , ai > ajai > aj, and for all k(k ≠ i, k < j)k(k ≠ i, k < j) ak < aj.ak < aj. We can compute this just looping through all jj and keeping the maximum over all elements but the .
Now note that it’s not required to fix aiai. rjrj can become 1 from 0 only when a certain element from the left is removed. Let’s loop through all ajaj and determine if there is an element to the left such that it is greater than ajaj, but all other elements are less than ajaj. We can check this using ordered set. If there is such a aiai, then increase xixi by 1. After the loop the array xixi is fully computed, so we can just find the element which brings the maximum number of records, and minimum among such.
一个元素比它前面的所有元素都大,称此元素为recordrecord。
首先用一个标记数组r[i]r[i]表示第i个元素是(1)否(0)是recordrecord,在O(N)O(N)时间内可以求出。
然后考虑移除一个元素ii后,能影响哪些?
自身。还有:在他之后的,某些原本不是recordrecord的,因为ii的移除,成为的。
这些recordrecord(称为jj)有这样的特点:比从1到除ii外的元素都大,但不比大。
记从1到j−1j−1除ii外的元素的最大值为,则可表示为k<j<=ik<j<=i。此不等式为本题“题眼”。
这样就有了官方的O(N2)O(N2)解法:枚举要移除的元素ii->检查及在他之后的jj,计算增量->得移除后能使recordrecord最大的元素。
然而BB半天,1e51e5数据,显然超时。
这时候想想“题眼”,那个神奇的不等式。
同样设(删除jj后的)增量数组,O(N)O(N)顺推并维护kk和不就可以了?
如果j>ij>i,说明jj是,x[j]−−x[j]−−,并更新jj为,kk为;
如果k<j=<ik<j=<i,“题眼”,x[i]++x[i]++(注意是ii),并更新为jj。
最后的问题,如果存在相同的元素,映射不唯一,一首凉凉?
回头审题,,计划通,代码非常简单。
小坑点,第一个元素是不算recordrecord的。
AC代码
#include<bits/stdc++.h>
using namespace std;
int a[100050];
int main()
{
int n;
while(cin>>n){
memset(a,0,sizeof(a));
int x,m2nd=0,m1st=0;
for(int i=1;i<=n;i++){
cin>>x;
if(x>m1st){
m2nd=m1st;
m1st=x;
a[x]--;
}
else if(x>m2nd){
m2nd=x;
a[m1st]++;
}
}
int ma=-0x3f3f3f3f,mi;
for(int i=1;i<=n;i++)
if(a[i]>ma)
ma=a[i],mi=i;
printf("%d\n",mi);
}
return 0;
}
506

被折叠的 条评论
为什么被折叠?



