【题解】Luogu P2757 等差子序列 hash

要求≥3,所以只需找3个

当这一位是a时,判断以a为中心的前后两段是否回文

若是,说明所有和a等差的项都出现过

如果不是,说明一项在前一项在后

因为是1到N的排列,所以一定存在这样的项

bitset代替线段树维护hash值

//
//  main.cpp
//  Luogu
//
//  Created by gengyf on 2019/7/6.
//  Copyright © 2019 yifan Geng. All rights reserved.
//

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define N 10010
int T,n,a;
bitset<N>bf,bb,bas,c,d;//bf_bitset_front bb_bitset_back
void clear(){
    bf.reset();bb.reset();bas.reset();
}
bool check(int w){
    if(w>n/2){
        c=bf>>(w-1);
        c.set(0);
        d=(bb>>(n-w))&(bas>>(w-1));
        d.set(0);
        if(d!=c)return true;
        else return false;
    }
    else {
        c=(bf&(bas>>(n-w)));
        c.set(0);
        d=((bb>>(n-2*w+1))&(bas>>(n-w)));
        d.set(0);
        if(c!=d)return true;
        else return false;
    }
}
int main(){
    scanf("%d",&T);
    while(T--){
        clear();
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            bas.set(i);
        }
        bool flag=false;
        for(int i=1;i<=n;i++){
            scanf("%d",&a);
            bf.set(a);bb.set(n-a+1);
            if(check(a))flag=true;
        }
        if(flag)printf("Y\n");
        else printf("N\n");
    }
    return 0;
}

 

posted @ 2019-07-07 12:06  喵の耳  阅读(162)  评论(0编辑  收藏  举报