# Sequence（思维题）

Problem Description
We define an element a_iai? in a sequence “good”, if and only if there exists a j(1\le j < i)j(1≤j小于i) such that a_j < a_iaj?小于ai?.
Given a permutation pp of integers from 11 to nn. Remove an element from the permutation such that the number of “good” elements is maximized.

Input
The input consists of several test cases. The first line of the input gives the number of test cases, T(1\le T\le 10^3)T(1≤T≤103).
For each test case, the first line contains an integer n(1\le n\le 10^6)n(1≤n≤106), representing the length of the given permutation.
The second line contains nn integers p_1,p_2,\cdots,p_n(1\le p_i\le n)p1?,p2?,?,pn?(1≤pi?≤n), representing the given permutation pp.
It’s guaranteed that \sum n\le 2\times 10^7∑n≤2×107.

Output
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.

Sample Input
2
1
1
5
5 1 2 3 4
Sample Output
1
5
Hint

Source
“浪潮杯”山东省第九届ACM大学生程序设计竞赛（感谢山东财经大学）

#include<bits/stdc++.h>
using namespace std;
const int N = 1000005;
typedef long long LL;
const int inf = 0x3f3f3f3f;

//不好的数肯定是他前面所有值中最小值
bool isgood[N];
int a[N];
int first[N];
int second[N];
int cnt[N];

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(isgood,true,sizeof(isgood));
int n;
scanf("%d",&n);
int MIN = inf;
int now = inf,pre = inf;
for(int i = 0;i < n;++i)
{
scanf("%d",&a[i]);
first[i] = now; second[i] = pre;
if(a[i] <= now){
pre = now;
now = a[i];
}
else{
if(a[i] < pre)
pre = a[i];
}
if(MIN > a[i]){
isgood[i] = false;
MIN = a[i];
}
}
//        for(int i = 0;i < n;++i)
//            cout << isgood[i] << endl;
//cnt存储的是去掉那个不好数，一定影响的好数的个数
//        for(int i = 0;i < n;++i)
//            cout << first[i] << " " << second[i] << endl;
int k = 0;
for(int i = 0;i < n;++i)
{
if(!isgood[i]){
k = i;
cnt[k] = 0;
}
else{
if(a[i] <= second[i])
cnt[k]++;
}
}
//        for(int i = 0;i < n;++i)
//            cout << cnt[i] << endl;
int tmp = inf;
for(int i = 0;i < n;++i)
{
if(!isgood[i] && cnt[i] == 0){
tmp = min(tmp,a[i]);
}
}
if(tmp == inf){
for(int i = 0;i < n;++i)
{
if(isgood[i]){
tmp = min(tmp,a[i]);
}
else{
if(cnt[i] == 1)
tmp = min(tmp,a[i]);
}
}
}
printf("%d\n",tmp);
}
return 0;
}


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

120