1. 程式人生 > >吉哥系列故事——完美隊形II HDU

吉哥系列故事——完美隊形II HDU

在擴充套件迴文半徑時加上判斷大小就好

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;

int tmp[maxn],ary[2*maxn],book[2*maxn];
int n;

int solve(int *ary,int n)
{
    int i,p,r,res;
    p=0,r=0,res=0;
    for(i=0;i<n;i++)
    {
        if(i<r) book[i]=min(book[2*p-i],r-i);
        else book[i]=1;
        while(0<=i-book[i]&&i+book[i]<n&&ary[i-book[i]]==ary[i+book[i]]&&(ary[i-book[i]]==-1||(ary[i-book[i]]<=ary[i-book[i]+2]&&ary[i+book[i]]<=ary[i+book[i]-2]))) book[i]++;
        if(r<=i+book[i]) p=i,r=i+book[i];
        res=max(res,book[i]);
    }
    return res;
}

int main()
{
    int t,i,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++) scanf("%d",&tmp[i]);
        j=0;
        for(i=0;i<n;i++) ary[j++]=-1,ary[j++]=tmp[i];
        ary[j++]=-1;
        n=j;
        printf("%d\n",solve(ary,n)-1);
    }
    return 0;
}