Problem Description
Given a permutation P of 1 to N (表明出现的数字从1到N,只是顺序打乱)
, YY wants to know whether there exists such three elements P[i1], P[i2], P[i3] that
P[i1]-P[i2]=P[i2]-P[i3], 1<=i1<i2<i3<=N.
P[i1]-P[i2]=P[i2]-P[i3], 1<=i1<i2<i3<=N.
Input
The 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.
Each test case comes two lines, the former one is N, 3<=N<=10000, the latter is a permutation of 1 to N.
Output
For each test case, just output 'Y' if such i
1, i
2, i
3 can be found, else 'N'
Sample Input
231 3 243 2 4 1
Sample Output
NY
由上面的红字可知,对于一个数p[i2] ,如果p[i1]在前面出现一次
hash[a]=1;
则p[i3]在后面肯定会出现,所以我们只需要找到p[i1](即hash[p[i1]]=1),并且要求p[i3]在前面没有出现(hash[p[i1]]=0) 或者反过来 于是
if(hash[a-j]+hash[a+j]==1) //表示找到了
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#define N 23541
using namespace std;
int main()
{
// priority_queue<int,vector<int>,greater<int> > q;
int num,m,a,hash[10000];
cin>>num;
while(num--)
{
cin>>m;
memset(hash,0,sizeof(hash));
int flag=0;
for(int i=0;i<m;i++)
{
cin>>a;
hash[a]=1;
if(!flag){
for(int j=1;a-j>0&&j+a<m;j++)
{
if(hash[a-j]+hash[a+j]==1)
{
flag=1;
break;
}
}
}
}
if(flag) cout<<"Y"<<endl;
else cout<<"N"<<endl;
}
return 0;
}