HDU 4513 Series Stories-Perfect Formation II (Manacher algorithm)

Source: Internet
Author: User

The story of Jill Series-Perfect Formation II

hdu-4513

Description

Gill again came up with a new perfect formation game!
Suppose there are n individuals standing in front of him in order, their height is h[1], h[2] ... h[n], Gill want to pick out some people, let these people form a new formation, the new formation if meet the following three points requirements, is the new perfect formation:

1, the selected person maintains the relative order of the original formation, and must be in the original formation of continuous;
2, symmetrical, false with m personal formation of a new form, then the 1th person and the personal height of the same, 2nd person and the first m-1 personal height is the same, and so on, of course, if M is odd, the middle of the person can be arbitrary;
3, from the left to the middle of the person, the height should be guaranteed not to decline, if the height of the new formation with H, then h[1] <= h[2] <= h[3] .... <= H[mid].

Now Gill want to know: How many people can be selected to form a new perfect formation?

Input

The first line of input data contains an integer t, indicating that there is a total of T-Group test data (T <= 20);
Each set of data is first an integer n (1 <= n <= 100000), representing the number of people in the original formation, followed by a line of n integers, indicating the height of the person standing from left to right in the original formation (<= H <= 250, not ruled out particularly short and tall).

Output

Please output the maximum number of people that make up the perfect formation, one row for each set of outputs.

Sample Input

2351 52 51451 52 52 51

Sample Output

34 since the person's height is above 50, then uses 0 and-one separates the array to make Manacher, in the algorithm judgment condition joins about the size judgment, transforms the palindrome string the smallest value, of course, this minimum value cannot be used to delimit the number.
#include <cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<Set>using namespacestd;Const intMAXN = 1e5 +7;intN;intS[MAXN];intss[maxn<<1];intp[maxn<<1];voidMake () {ss[0] = -1; intpos =0;  for(inti =0; I < n; i++) {ss[++pos] =0; ss[++pos] =S[i]; }ss[++pos] =0; N= n *2+2; //for (int i = 0; i < n; i++) printf ("%d", ss[i]); Puts ("");}voidsolve () {intMaxx =0, id;  for(inti =1; I < n; i++) {        if(Maxx >i) p[i]= Min (P[id *2-i], P[id] + ID-i); ElseP[i] =1; intMin; if(Ss[i] >0) Min =Ss[i]; ElseMin = Ss[i +1];  while(Ss[i-p[i] [= Ss[i + p[i]] && Ss[i-p[i]] <=Min) {            //printf ("Is You here?\n");            if(Ss[i-p[i]) >0) Min= Ss[i-P[i]]; if(i + p[i] >Maxx) {Maxx= i +P[i]; ID=i; } P[i]++; }    }    intAns =0;  for(inti =1; I < n; i++) ans =Max (ans, p[i]); printf ("%d\n", ans-1);}intMain () {intT; scanf ("%d", &t);  while(t--) {scanf ("%d", &N);  for(inti =0; I < n; i++) {scanf ("%d", &S[i]);        } make ();    Solve (); }}

HDU 4513 Series Stories-Perfect Formation II (Manacher algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.