多校第七场 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 );
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值