Hotaru's problem
HDU - 5371
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.
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.
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
We guarantee that the sum of all answers is less than 800000.
1 10 2 3 4 4 3 2 2 3 4 4
Case #1: 9
刚开始一直搞不懂为什么会有大于,觉得应该是等于
例子
321123321123
#3#2#1#1#2#3#3#2#1#1#2#3#
i j
这个情况a[i]/2 > x
j = i+x
a[j]/2 = x;
code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 200017
int N;
int p[maxn];
int a[maxn];
int b[maxn];
int n;
void init()
{
int i;
for(i = 0; i < n; i ++)
{
b[2 * i + 1] = -1;
b[2 * i + 2] = a[i];
}
N = 2 * i + 1;
b[0] = -2;
b[N] = b[N + 1] = -1;
}
void manacher()
{
int id;
int maxx = 0;
int ans = 0;
for(int i = 1; i <= N; i ++)
{
if(maxx > i)
p[i] = min(maxx-i, p[2*id-i]);
else
p[i] = 1;
while(b[i + p[i]] == b[i - p[i]])
++ p[i];
if(i + p[i] > maxx)
{
maxx = i + p[i];
id = i;
}
}
}
int main()
{
int t;
int cas = 0;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
init();
manacher();
int ans = 1;
for(int i = 3; i < N; i+=2)
{
for(int j = ans; j <= p[i]; j+=2)
{
if(p[i+j-1] >= j)
{
ans = j;
}
}
}
ans = ans/2*3;
printf("Case #%d: %d\n",++cas,ans);
}
return 0;
}