题意:对一个长度为n的排列,移除一个数字,使得record数的个数最多,record数是比它前面所有的数都大的数。
思路:想了很久还是没有想到怎么做,因为1e5的数据有点大。删除一个数之后,对于它后面的数来说,能不能成为一个record数决定于它是不是第二大的数,如果它是就能成为一个record数,否则就不能。所以只需要维护每个数的前缀的最大值和第二大值就可以了。
#pragma warning(disable:4996)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[100005],cnt[100005];
//cnt代表把这个数删除带来的贡献
int main()
{
int MAX1, MAX2, i, j, ans, MAX, n;
while (scanf("%d", &n) == 1)
{
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
MAX1 = 0; MAX2 = 0;
ans = 0;
memset(cnt, 0, sizeof(cnt));
for (i = 0; i < n; i++)
{
if (MAX1 < a[i])
{
MAX2 = MAX1;
MAX1 = a[i];//更新最大最小值
cnt[a[i]]--;//删除当前最大值会使得record数减一
//因为最大值也是record数
}
else if (MAX2 < a[i])
{
MAX2 = a[i];
cnt[MAX1]++;//删除最大值会使得record加一
//因为当前数会成为一个record数
}
}
MAX = -100005;
for (i = 1; i <= n; i++)
{
if (MAX < cnt[i])
{
MAX = cnt[i];
ans = i;
}
}
printf("%d\n", ans);
}
return 0;
}