多校第七场 1003 hdu 5371 Hotaru's problem(manacher+set)

AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <set>
#define MAX 100007

using namespace std;

int t,n;
set<int> s1;
set<int,greater<int> >s2;
struct Node
{
    int v,id;
    bool operator < ( const Node& a ) const
    {
        return v > a.v;
    }
}a[MAX];
int b[MAX];
int temp[MAX<<1];
int Len[MAX<<1];

int init ( int * st , int n )
{
    int i;
    temp[0] = -1;
    for ( int i = 1 ; i <= 2*n ; i += 2 )
    {
        temp[i] = -2;
        temp[i+1] = st[i/2];
    }
    temp[2*n+1] = -2;
    temp[2*n+2] = -3;
    temp[2*n+3] = 0;
    return 2*n+1;
}


void manacher ( int *st , int len )
{
    int mx = 0 , ans = 0 , po = 0;
    for ( int i = 1 ; i <= len ; i++ )
    {
        if ( mx > i )
            Len[i] = min ( mx - i , Len[2*po-i] );
        else
            Len[i] = 1;
        while ( st[i-Len[i]] == st[i+Len[i]] )
            Len[i]++;
        if ( Len[i]+i > mx )
            mx = Len[i]+i , po = i;
    }
}

int main ( )
{
    scanf ( "%d" , &t );
    int cc = 1;
    while ( t-- )
    {
        scanf ( "%d" , &n );
        for ( int i = 0; i < n ; i++ )
        {
            scanf ( "%d" , &b[i] );
            a[i].id = i;
        }
        manacher ( temp , init ( b , n ));
        s1.clear();
        s2.clear();
        for ( int i = 0 ; i < n ; i++ )
        {
            //a[i].v = Len[(i+1)*2]-1;
            a[i].v = Len[(i+1)*2+1]-1;
        }
        sort ( a , a+n );
        int ans = 0;
        for ( int i = 0 ; i < n ; i++ )
        {
            s1.insert ( a[i].id );
            s2.insert ( a[i].id );
            int x = a[i].id-a[i].v/2;
            int j = *s1.lower_bound ( x );
            /*if ( i== 2 ) { cout << "---------------------------" << endl; cout <<x << " " << j << " " << a[i].id << endl; }*/
            ans = max ( a[i].id-j , ans );
            x = a[i].id+a[i].v/2;
            j = *s2.lower_bound ( x );
            ans = max ( j-a[i].id , ans );
            /*if ( i == 2 ) { cout <<x << " " << j << " " << a[i].id << endl; cout << "----------------------------" << endl; } cout << i << " " << ans << endl;*/
        }
        printf ( "Case #%d: %d\n" , cc++ , ans*3 );
    }
}

你可能感兴趣的:(set,Manacher,多校)