刚开始的时候虽然也用了哈希,但复杂度是n^n的,所以果断的tle了。
搜了一篇题解,顿时,膜拜之意油然而生,虽然复杂度都是n^n的,但是大神的代码果断的优化了。把复杂度从10^8降到了10^7了。
同样使用哈希,达到的效果果断的有天差地别呀。。。
代码如下:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <map>
#include <algorithm>
#define LL long long
#define LLU unsigned long long
#define INF 0x7fffffff
#define MOD 100000007
using namespace std;
#define M 10005
int hash[M];
int main ()
{
int t, n, x;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(hash,0,sizeof(hash));
int flag = 1;
for(int i = 1; i <= n; ++i)
{
scanf("%d",&x);
hash[x] = 1;
if(flag)
for(int j = 1; x-j>=1&&x+j<=n; ++j)
if(hash[x-j]+hash[x+j]==1)
{ flag = 0; break; }
}
printf("%s\n",flag==0?"Y":"N");
}
return 0;
}