YY's new problem
Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4968 Accepted Submission(s): 1388
Problem Description
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.
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
2 3 1 3 2 4 3 2 4 1
Sample Output
N Y
Source
Recommend
ac代码
#include<stdio.h>
#include<string.h>
int hash[10010];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
int flag=0;
memset(hash,0,sizeof(hash));
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
hash[x]=1;
if(flag)
continue;
for(int j=1;j<x&&j+x<=n;j++)
{
if(hash[x-j]+hash[x+j]==1)
{
flag=1;
break;
}
}
}
if(flag)
printf("Y\n");
else
printf("N\n");
}
}