HDU3833 YY's new problem 卡时间第一题
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[10010],a[10010];
int main()
{
int i,j,n,T;
scanf("%d",&T);
while(T--){
memset(vis,0,sizeof(vis));
bool flag=false;
scanf("%d",&n);
for(i=1;i<=n;i++)scanf("%d",&a[i]);
for(i=1;i<=n&&!flag;i++){
vis[a[i]]=1;
for(j=1;j<a[i];j++) {
if(j+a[i]<=n&&vis[a[i]-j]+vis[a[i]+j]==1){
flag=true;
break;
}
}
}
if(flag) printf("Y\n");
else printf("N\n");
}
return 0;
}
It is your time to fight!