关闭

HDU--杭电--3833--YY's new problem

956人阅读 评论(0) 收藏 举报
分类:

YY's new problem

Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3434    Accepted Submission(s): 961


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
 
题意:在数列中能否找到等差数列,而且  i1+i3=2*i2 ,   i1+i3只能是偶数

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main (void)
{
    int t,n,m,i,j,k,l,s[11111],hash[11111];
    scanf("%d",&t);
    while(t--&&scanf("%d",&n)!=EOF)
    {
        memset(hash,0,sizeof(hash));
        for(i=0;i<n;i++)
        {
            scanf("%d",&s[i]);
            hash[s[i]]=i;	//记录当前数所在的位子
        }
        for(i=k=0;!k&&i<n;i++)
        {
            if(i<n/2)
            for(j=0;j<i;j++)	//前半段通过i1,i2找i3
            {
                l=2*s[i]-s[j];
                if(l>0&&l<=n&&hash[l]>i)
                {
                    k=1;break;
                }
            }else
            for(j=n-1;!k&&j>i;j--)	//后半段通过i2,i3找i1
            {
                l=2*s[i]-s[j];
                if(l>0&&l<=n&&hash[l]<i)
                {
                    k=1;break;
                }
            }
        }
        if(k)puts("Y");
        else puts("N");
    }
    return 0;
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:173014次
    • 积分:3882
    • 等级:
    • 排名:第8476名
    • 原创:219篇
    • 转载:1篇
    • 译文:0篇
    • 评论:20条
    最新评论