• HDU3833 YY's new problem 卡时间第一题


    Given a permutation P of 1 to N, YY wants to know whether there exists such three elements P[i 1], P[i 2], P[i 3] that 
    P[i 1]-P[i 2]=P[i 2]-P[i 3], 1<=i 1<i 2<i 3<=N.

    InputThe 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.OutputFor each test case, just output 'Y' if such i 1, i 2, i 3 can be found, else 'N'.Sample Input

    2
    3
    1 3 2
    4
    3 2 4 1

    Sample Output

    N
    Y

    不稍微优化一下很容易超时:

    # include<iostream>
    # include<cstdio>
    # include<string.h>
    # include<algorithm>
    using namespace std;
    int vis[10010],a[10010];
    int main()
    {
        int i,j,n,T;
        scanf("%d",&T);
        while(T--){
            memset(vis,0,sizeof(vis));
            bool flag=false;
            scanf("%d",&n);
            for(i=1;i<=n;i++)scanf("%d",&a[i]);
            for(i=1;i<=n&&!flag;i++){
                 vis[a[i]]=1;
                 for(j=1;j<a[i];j++) {
                        if(j+a[i]<=n&&vis[a[i]-j]+vis[a[i]+j]==1){
                              flag=true;
                              break;
                        }
                 }
            }
            if(flag) printf("Y
    ");
            else printf("N
    ");
        }
        return 0;
    }
    View Code

     

  • 相关阅读:
    增加文章
    网站之注册
    C#常用的引用
    Session.Abandon和Session.Clear有何不同 (转)
    C#文件路径的写法
    UpdatePanel的用法详解
    [转]asp:ScriptManager
    Git 常用命令
    AJAX请求 $.post方法的使用
    a 标签中调用js的几种方法
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7711294.html
Copyright © 2020-2023  润新知