Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef vector<int>::iterator VI;
const int maxn=110000;
const int INF=0x3f3f3f3f;
int a[maxn],n,dp1[maxn],dp2[maxn],num1[maxn],num2[maxn];
void doit(int dp[],int num[])
{
memset(dp,0,sizeof(dp));
memset(num,0,sizeof(num));
vector<int> g; g.clear();
int sz=0;
for(int i=n-1;i>=0;i--)
{
if(!sz||g[sz-1]<=a[i])
{
g.push_back(a[i]); sz++;
dp[i]=sz;
}
else
{
VI item;
item=upper_bound(g.begin(),g.end(),a[i]);
dp[i]=item-g.begin()+1;
*item=a[i];
}
pair<VI,VI> bound=equal_range(g.begin(),g.end(),a[i]);
num[i]=bound.second-bound.first;
}
}
int main()
{
int T,ans;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
ans=1;
for(int i=0;i<n;i++) scanf("%d",a+i);
doit(dp1,num1);
for(int i=0;i<n;i++) a[i]*=-1;
doit(dp2,num2);
for(int i=0;i<n;i++)
{
ans=max(ans,dp1[i]+dp2[i]-min(num1[i],num2[i]));
}
printf("%d\n",ans);
}
return 0;
}