HDU3833——Hash算法

Problem Description

Given a permutation P of 1 to 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 i1, i2, i3 can be found, else 'N'.

Sample Input

2

3

1 3 2

4

3 2 4 1

Sample Output

N

Y

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3833

#include<iostream>
#include<cstdio>
using namespace std;
const int N=10005;
int a[N];
int ahash[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            ahash[a[i]]=i;
        }
        int flag=0;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                int sum=a[i]+a[j];
                if(sum%2)
                    continue;
                if(ahash[sum/2]>i&&ahash[sum/2]<j)
                {
                    flag=1;break;
                }
                if(flag)
                    break;
            }
        }
        if(flag)
            cout<<"Y"<<endl;
        else
            cout<<"N"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/salmonwilliam/article/details/81175980