hdu 3833 YY’s new problem

hdu 3833 YY’s new problem

/*
此题让我头疼到无语
错误百出
WA了不下十次
for a beginner 
这道题让我体会到了万念俱灰的感觉 
先说一下思路:
题目中说,给定一个N
然后在给定一个1...N的序列
也就是说1...N每个数都会出现
且只会出现一次
这是其一
引入hash[]来记录该数是否已经出现
出现为1,否则为0
读入一个数t
从1到t-1依次判断是否有hash[t-i]+hash[t+i]==1
即以t为中项,对于t-i,t+i是否仅出现过一个由于是按顺序读入的
即可保证t-i和t+i在原序列中一定是在t的两边
amazing! 
*/
#define LOCAL
#include<iostream>
#include<cstring>
#define N 10005
using namespace std;
bool hash[N],flag;
int main()
{
#ifdef LOCAL
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
#endif
    
    int n,m,i,t,j;
    cin>>m;
    while(m--)
    {
           cin>>n;
           memset(hash,0,sizeof(hash));
           j=n;
           flag=0;
           while(j--)
           {
                   cin>>t;
                   hash[t]=1;    //这个不能放里边,否则会出现有些数据无法读给hash[]的情况 
                   for(i=1;!flag&&(i<t)&&(t+i<=n);i++)
                   {
                           if(hash[t-i]+hash[t+i]==1)
                                 flag=1;   //设定标志变量flag,一旦出现符合条件的组合就跳出以节省运行时间 
                    }
            }    
            if(flag)
                   cout<<"Y"<<endl;
            else 
                   cout<<"N"<<endl;
    }
    return 0;
}

发表回复