zoukankan      html  css  js  c++  java
  • 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

     

  • 相关阅读:
    Java 之 Maven 基础
    JavaScript 之 RegExp 对象
    Java 之 Jedis
    Java 之 Redis 基础
    Java 之 NOSQL
    JavaWeb 之 JSON
    JavaWeb 之 Ajax
    【LeetCode-数组】外观数列
    【LeetCode-树】从先序遍历还原二叉树
    【LeetCode-数组】搜索二维矩阵 II
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7711294.html
Copyright © 2011-2022 走看看