hdu 3833 YY's new problem hash

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.
 

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.
 

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值