现在的位置: 首页 > 综合 > 正文

hdu4513—吉哥系列故事——完美队形II

2019年02月14日 ⁄ 综合 ⁄ 共 2242字 ⁄ 字号 评论关闭

Problem Description
  吉哥又想出了一个新的完美队形游戏!
  假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] … h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要求,则就是新的完美队形:

  1、挑出的人保持原队形的相对顺序不变,且必须都是在原队形中连续的;
  2、左右对称,假设有m个人形成新的队形,则第1个人和第m个人身高相同,第2个人和第m-1个人身高相同,依此类推,当然如果m是奇数,中间那个人可以任意;
  3、从左到中间那个人,身高需保证不下降,如果用H表示新队形的高度,则H[1] <= H[2] <= H[3] …. <= H[mid]。

  现在吉哥想知道:最多能选出多少人组成新的完美队形呢?

Input
  输入数据第一行包含一个整数T,表示总共有T组测试数据(T <= 20);
  每组数据首先是一个整数n(1 <= n <= 100000),表示原先队形的人数,接下来一行输入n个整数,表示原队形从左到右站的人的身高(50 <= h <= 250,不排除特别矮小和高大的)。

Output
  请输出能组成完美队形的最多人数,每组输出占一行。

Sample Input

2 3 51 52 51 4 51 52 52 51

Sample Output

3 4

Source
2013腾讯编程马拉松初赛第二场(3月22日)

Recommend
liuyiding | We have carefully selected several similar problems for you: 5177 5173 5169 5168 5165

先通过manacher求出以每个位置为中心的回文串的长度
设dp[i] 为 到i位置的最长单调不下降区间长度
if (arr[i] >= arr[i - 1])
dp[i] = dp[i - 1] + 1;
else
dp[i] = 1;
然后for一遍找到最大值即可

/*************************************************************************
    > File Name: hdu4513.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年02月18日 星期三 13时15分31秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 100010;

int p[N << 1];
int arr[N << 1];
int pos[N];
int dp[N];

void manacher (int cnt)
{
    memset (p, 0, sizeof(p));
    int MaxId = 0;
    int id;
    for (int i = 1; i < cnt; ++i)
    {
        if (MaxId > i)
        {
            p[i] = min (p[2 * id - i], MaxId - i);
        }
        else
        {
            p[i] = 1;
        }
        while (arr[i + p[i]] == arr[i - p[i]])
        {
            ++p[i];
        }
        if (p[i] + i > MaxId)
        {
            MaxId = p[i] + i;
            id = i;
        }
    }
}

int main ()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        arr[0] = -2;
        arr[1] = -1;
        int cnt = 2;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d", &pos[i]);
            arr[cnt++] = pos[i];
            arr[cnt++] = -1;
        }
        arr[cnt] = -3;
        manacher (cnt);
        dp[1] = 1;
        for (int i = 2; i <= n; ++i)
        {
            if (pos[i] >= pos[i - 1])
            {
                dp[i] = dp[i - 1] + 1;
            }
            else
            {
                dp[i] = 1;
            }
        }
        dp[0] = 0;
        int ans = 0;
        for (int i = 1; i <= cnt; ++i)
        {
            if (arr[i] == -1)
            {
                ans = max (ans, min (dp[i / 2] * 2, p[i] - 1));
            }
            else
            {
                ans = max (ans, min (dp[i / 2] * 2 - 1, p[i] - 1));
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

抱歉!评论已关闭.