# 第九届山东省赛E题 Sequence II 【思维题】

## 题目描述

We define an element ai in a sequence "good", if and only if there exists a j(1≤ j < i) such that aj < ai.
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, T(1≤ T≤ 10^3).
For each test case, the first line contains an integer n(1≤ n≤ 10^6), representing the length of the given permutation.
The second line contains n integers p1,p2,\cdots,pn(1≤ pi≤ n), representing  the given permutation p.
It’s guaranteed that Σn≤ 2× 10^7.

## 输出

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.

## 样例输入

2
1
1
5
5 1 2 3 4


## 样例输出

1
5


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN = 1e6+5;
const int INF = 0x3f3f3f3f;
int n;
int num[MAXN];
int vis[MAXN];///记录代价值
int zx,cx,zxi,cxi;///zx是最小值。cx是次小值，zxi是最小值的下标。cxi是次小值的下标。
{
char c;
int ret = 0;
while ((c = getchar()) < '0' || c > '9');
while (c >= '0' && c <= '9')
{
ret = ret * 10 + (c - '0'), c = getchar();
}
return ret;
}
int main()
{
int T;
while(T--)
{
M(vis,0);

for(int i=0; i<n; i++)  num[i] = read();
zx = INF;
cx = INF;
zxi=0;
cxi=0;
for(int i=0;i<n;i++)
{
if(num[i]>cx)///与次小值比较
{
vis[i]++;
}
else if(num[i]>zx)///如果大于最小值且小于次小值
{
vis[i]++;
vis[zxi]++;///最小值的代价增加
}
if(num[i]<zx)///小于最小值，更新最小值与次小值。
{
cx = zx;
cxi = zxi;
zx = num[i];
zxi = i;
}
else if(num[i]<cx)///只小于次小值，更新次小值。
{
cx = num[i];
cxi = i;
}

}
// for(int i=0; i<n; i++) printf("vis[%d]==%d\n",i,vis[i]);
int M1 = vis[0];
int M2 = num[0];
for(int i=1; i<n; i++)
{
if(vis[i]<M1)///选取代价最小
{
M1=vis[i];
M2=num[i];
}
if(vis[i]==M1&&num[i]<M2)///代价相等，取数小的。
{
M2 = num[i];
}
}
printf("%d\n",M2);
}
return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120