Given a permutation P of 1 to N, YY wants to know whether there exists such three elements P[i 1], P[i 2], P[i 3] that
P[i 1]-P[i 2]=P[i 2]-P[i 3], 1<=i 1<i 2<i 3<=N.
P[i 1]-P[i 2]=P[i 2]-P[i 3], 1<=i 1<i 2<i 3<=N.
InputThe first line is T(T<=60), representing the total test cases.
Each test case comes two lines, the former one is N, 3<=N<=10000, the latter is a permutation of 1 to N.OutputFor each test case, just output 'Y' if such i 1, i 2, i 3 can be found, else 'N'.Sample Input
2
3
1 3 2
4
3 2 4 1
Sample Output
N
Y
不稍微优化一下很容易超时:
# include<iostream>
# include<cstdio>
# include<string.h>
# include<algorithm>
using namespace std;
int vis[],a[];
int main()
{
int i,j,n,T;
scanf("%d",&T);
while(T--){
memset(vis,,sizeof(vis));
bool flag=false;
scanf("%d",&n);
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=n&&!flag;i++){
vis[a[i]]=;
for(j=;j<a[i];j++) {
if(j+a[i]<=n&&vis[a[i]-j]+vis[a[i]+j]==){
flag=true;
break;
}
}
}
if(flag) printf("Y\n");
else printf("N\n");
}
return ;
}