Files
OI-source/2.1618.cpp
2023-08-03 09:22:52 +08:00

76 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=5005;
int a[maxn],l[maxn][maxn],r[maxn][maxn];
// l[i][j]±íʾϱêСÓÚjÇÒÖµ±ÈNi´óµÄÊýÖÐ×îСֵµÄλÖÃ
// r[i][j]±íʾϱê´óÓÚjÇÒÖµ±ÈNiСµÄÊýÖÐ×î´óÖµµÄλÖÃ
int k;
bool solve()
{
for(int i=1;i<=k;i++)
{
l[i][0]=0;
for(int j=1;j<i;j++)
{ // ö¾ÙNq,ÕÒNp
if(a[i]>=a[j])
l[i][j]=l[i][j-1];
else if(!l[i][j-1]||a[j]<a[l[i][j-1]])
l[i][j]=j;
else
l[i][j]=l[i][j-1];
}
}
for(int i=1;i<=k;i++)
{
r[i][k+1]=0;
for(int j=k;j>i;j--)
{// ö¾ÙNr,ÕÒNs
if(a[i]<=a[j])
r[i][j]=r[i][j+1];
else if(!r[i][j+1]||a[j]>a[r[i][j+1]])
r[i][j]=j;
else
r[i][j]=r[i][j+1];
}
}
//ö¾Ùr£¬q£¬ÕÒp£¬s¾Í±ä³ÉÁ˳£Êýʱ¼ä¡£¸´ÔÓ¶ÈÓÉn^4±ä³ÉÁËn^2.
for(int i=1;i<=k;i++)
{
for(int j=i+1;j<=k;j++)
{
if(!l[j][i-1]||!r[i][j+1]||a[i]<=a[j])
continue;
int p=l[j][i-1],s=r[i][j+1];
if(a[j]<a[p]&&a[p]<a[s]&&a[s]<a[i])
return true;
}
}
return false;
}
int main()
{
#ifdef local
freopen("pro.in","r",stdin);
#endif
ios::sync_with_stdio(false);
int T;
cin>>T;
while(T--)
{
cin>>k;
for(int i=1;i<=k;i++) cin>>a[i];
if(solve()) cout<<"YES\n";
else
{
reverse(a+1,a+k+1);
if(solve()) cout<<"YES\n";
else cout<<"NO\n";
}
}
return 0;
}