HDU 5371 Hotaru's problem

manacher算法介绍

先用求回文串的Manacher算法,求出以第i个点和第i+1个点为中心的回文串长度,记录到数组c中 比如 10 9 8 8 9 10 10 9 8 我们通过运行Manacher求出第i个点和第i+1个点为中心的回文串长度 0 0 6 0 0 6 0 0 0

两个8为中心,10 9 8 8 9 10是个回文串,长度是6。 两个10为中心,8 9 10 10 9 8是个回文串,长度是6。

要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,比如上边的两个字符串,共享 8 9 10这一部分。 也就是说,左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也是一样。 因为我们已经记录下来以第i个点和第i+1个点为中心的回文串长度, 那么问题可以转化成,相距x的两个数a[i],a[i+x],满足a[i]/2>=x 并且 a[i+x]/2>=x,要求x尽量大

这可以用一个set维护,一开始集合为空,依次取出a数组中最大的元素,将其下标放入set中,每取出一个元素,由于set里面的下标回文半径都>=他的回文半径,在该集合中二分查找 <= i+a[i]/2,但最大的元素,更新答案。 然后查找集合中 >= i-a[i]/2,但最小的元素,更新答案。

答案就是3*an

Hotaru's problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1384    Accepted Submission(s): 498


Problem Description
Hotaru Ichijou recently is addicated to math problems. Now she is playing with N-sequence.
Let's define N-sequence, which is composed with three parts and satisfied with the following condition:
1. the first part is the same as the thrid part,
2. the first part and the second part are symmetrical.
for example, the sequence 2,3,4,4,3,2,2,3,4 is a N-sequence, which the first part 2,3,4 is the same as the thrid part 2,3,4, the first part 2,3,4 and the second part 4,3,2 are symmetrical.

Give you n positive intergers, your task is to find the largest continuous sub-sequence, which is N-sequence.
 

Input
There are multiple test cases. The first line of input contains an integer T(T<=20), indicating the number of test cases. 

For each test case:

the first line of input contains a positive integer N(1<=N<=100000), the length of a given sequence

the second line includes N non-negative integers ,each interger is no larger than  109  , descripting a sequence.
 

Output
Each case contains only one line. Each line should start with “Case #i: ”,with i implying the case number, followed by a integer, the largest length of N-sequence.

We guarantee that the sum of all answers is less than 800000.
 

Sample Input
   
   
   
   
1 10 2 3 4 4 3 2 2 3 4 4
 

Sample Output
   
   
   
   
Case #1: 9
 

Source
2015 Multi-University Training Contest 7
 

#include <bits/stdc++.h>
using namespace std;
#define prt(k) cerr<<#k" = "<<k<<endl
typedef long long ll;
const ll inf = 0x3f3f3f3f;
const int N = 101000;
int str[N],ans[N<<1];
int p[N<<1],pos,how;
int n;
void manacher()
{
    pos=-1;how=0;
    memset(p,0,sizeof(p));
    int len=2*n+2;
    int mid=-1,mx=-1;
    for(int i=0;i<len;i++)
    {
        int j=-1;
        if(i<mx)
        {
            j=2*mid-i;
            p[i]=min(p[j],mx-i);
        }
        else p[i]=1;

        while(i+p[i]<len&&ans[i+p[i]]==ans[i-p[i]])
        {
            p[i]++;
        }

        if(p[i]+i>mx)
        {
            mx=p[i]+i; mid=i;
        }
        if(p[i]>how)
        {
            how=p[i]; pos=i;
        }
    }
}
void pre()
{
    memset(ans,0,sizeof ans);
    ans[0] = -1;
    ans[1] = -2;
    for (int i=0;i<n;i++) {
        ans[2*i+2] = str[i];
        ans[2*i+3] = -2;
    }
    ans[2*n+2] = 0;
    manacher();
    for (int i=3;i<2*n+2;i+=2) {
        p[(i-3)/2] = (p[i] - 1) / 2;
    }
}
struct P
{
    int a, id;
};
P a[N<<1];
bool cmp(P a, P b)
{
    return a.a > b.a;
}
int main()
{
    int re; scanf("%d", &re); int ca = 1;
    while (re--) {
        scanf("%d", &n);
        for (int i=0;i<n;i++) scanf("%d", &str[i]);
        printf("Case #%d: ", ca++);
        if (n < 3) {
            puts("0");
            continue;
        }
        pre();
        int ans = 0;
        for (int i=0;i<n;i++) {
           // printf("p[%d] = %d\n", i, p[i]);
            a[i].a = p[i];
            a[i].id = i;
        }
        set<int> se;
        sort(a, a+n, cmp);
        for (int i=0;i<n;i++) {
            auto it = se.upper_bound(a[i].id+a[i].a);
            if (it != se.begin() ) {
                it--;
                if(*it - a[i].id <= a[i].a);
                ans = max(ans, *it - a[i].id);
            }
            it = se.lower_bound(a[i].id - a[i].a);
            if (it != se.end()) {
                ans = max(ans, a[i].id - *it);
            }
            se.insert(a[i].id);
        }
        printf("%d\n",  3*ans);
    }
    return 0;
}
/**
34
9
10 9 8 8 9 10 10 9 8

*/


你可能感兴趣的:(HDU 5371 Hotaru's problem)