zoukankan      html  css  js  c++  java
  • HDU4513 吉哥系列故事——完美队形II Manacher算法

    题目链接:https://vjudge.net/problem/HDU-4513

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

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 3697    Accepted Submission(s): 1480


    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
     
    Recommend
    liuyiding

    题解:

    比普通的Manacher算法多了一个限制条件。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <string>
     6 #include <vector>
     7 #include <map>
     8 #include <set>
     9 #include <queue>
    10 #include <sstream>
    11 #include <algorithm>
    12 using namespace std;
    13 typedef long long LL;
    14 const double eps = 1e-6;
    15 const int INF = 2e9;
    16 const LL LNF = 9e18;
    17 const int MOD = 1e9+7;
    18 const int MAXN = 1e6+10;
    19 
    20 int s[MAXN], Ma[MAXN<<1];
    21 int Mp[MAXN<<1];
    22 
    23 int Manacher(int *s, int len)
    24 {
    25     int ret = 0;
    26     int l = 0;
    27     Ma[l++] = -INF; Ma[l++] = INF;
    28     for(int i = 0; i<len; i++)
    29     {
    30         Ma[l++] = s[i];
    31         Ma[l++] = INF;
    32     }
    33     Ma[l] = 0;
    34 
    35     int mx = 0, id = 0;
    36     for(int i = 1; i<l; i++)
    37     {
    38         Mp[i] = mx>=i?min(Mp[2*id-i], mx-i):0;
    39         while(Ma[i-Mp[i]-1]==Ma[i+Mp[i]+1] && Ma[i+Mp[i]+1]<=Ma[i+Mp[i]+1-2])
    40             Mp[i]++;
    41         if(i+Mp[i]>mx)
    42         {
    43             mx = i+Mp[i];
    44             id = i;
    45         }
    46         ret = max(ret,Mp[i]);
    47     }
    48     return ret;
    49 }
    50 
    51 int main()
    52 {
    53     int T, n;
    54     scanf("%d", &T);
    55     while(T--)
    56     {
    57         scanf("%d", &n);
    58         for(int i = 0; i<n; i++)
    59             scanf("%d", &s[i]);
    60 
    61         int ans = Manacher(s, n);
    62         printf("%d
    ", ans);
    63     }
    64 }
    View Code
  • 相关阅读:
    linux 查看父进程号
    gitlab
    诺基亚C6常识详解
    C#.net书籍列表
    表链接
    Limu:JavaScript的那些书(转载)
    Oracle 多行记录合并/连接/聚合字符串的几种方法
    关于有锁iPhone的常识(转载)
    not in与not exists性能比较
    多表连接查询
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7891879.html
Copyright © 2011-2022 走看看