We define an element in a sequence "good", if and only if there exists
(1≤j<i) such that
.
Given a permutation p of integers from 1 to n. Remove an element from the permutation such that the number of "good" elements is maximized.
输入描述:
The input consists of several test cases. The first
line of the input gives the number of test cases,.
For each test case, the first line contains an
integer ,
representing the length of the given permutation.
The second line contains n integersp1,p2,…,pn ,
representing the given permutation p.
It's guaranteed that .
输出描述:
For each test case, output one integer in a single
line, representing the element that should be deleted. If there are several
answers, output the minimal one.
示例1
输入
2
1
1
5
5 1 2 3 4
输出
1
5
题目大意:好数:在该数前面有比它小的数为好数(下标和值都比它小),要求删除一个数,求删除后好数最多的数,若有多个,输出最小的那个。
思路:跑一边维护最小值与次小值。
代码如下:
#include <bits/stdc++.h>
using namespace std;
const int MAXN=1000005;
int cnt[MAXN];
int main()
{
int t;
while(~scanf("%d",&t))
{
while(t--)
{
int n; scanf("%d",&n);
int min1=MAXN,min2=MAXN;
for(int i=0;i<n;i++)
{ // cnt[i] 表示删除i后会减少的good数的数量
int x; scanf("%d",&x); cnt[x]=0;
if(min1<x && x<min2) cnt[min1]++;
// x确定是因min1而成为一个good数
// 删除min1 就会少了x这个good数
if(min1<x) cnt[x]++;
// x确定是一个good数
// 删除x 至少会少了其本身这个good数
if(x<min1) min2=min1,min1=x;
else if(x<min2) min2=x;
}
int mini=MAXN,ans;
for(int i=1;i<=n;i++)
{ // 选出cnt[i]最小的即可
if(cnt[i]==mini) ans=min(ans,i);
else if(cnt[i]<mini) mini=cnt[i], ans=i;
}
printf("%d\n",ans);
}
}
return 0;
}