HDU-4513-完美队形2(Manacher变形)
链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4513
题意:
吉哥又想出了一个新的完美队形游戏!
假设有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]。
现在吉哥想知道:最多能选出多少人组成新的完美队形呢?
思路:
马拉车算法扩展时增加判断条件, 不满足条件时, 直接退出.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 2e5+10;
int a[MAXN], b[MAXN];
int Hw[MAXN];
int n;
void Manacher(int *ori, int len)
{
int maxr = 0, mid;
for (int i = 1;i < len;i++)
{
if (i < maxr)
Hw[i] = min(Hw[mid*2-i], maxr-i);
else
Hw[i] = 1;
while (ori[i-Hw[i]] == ori[i+Hw[i]])
{
if (ori[i-Hw[i]] != -1 && ori[i-Hw[i]] > ori[i-Hw[i]+2])
break;
Hw[i]++;
}
if (Hw[i]+i > maxr)
{
maxr = i+Hw[i];
mid = i;
}
}
}
void Change(int *ori, int *pet, int len)
{
pet[0] = pet[1] = -1;
for (int i = 0;i < len;i++)
{
pet[i*2+2] = ori[i];
pet[i*2+3] = -1;
}
pet[len*2+2] = 0;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
memset(b, 0, sizeof(b));
scanf("%d", &n);
for (int i = 0;i < n;i++)
scanf("%d", &a[i]);
Change(a, b, n);
Manacher(b, n*2+2);
int ans = 0;
for (int i = 0;i < n*2+2;i++)
ans = max(ans, Hw[i]);
printf("%d\n", ans-1);
}
return 0;
}
HDU-4513-完美队形2(Manacher变形)的更多相关文章
- Hdu 4513 吉哥系列故事——完美队形II (manacher变形)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4513 题目描述: 打完题目描述了,点开题目,发现题目是中文,orz.jpg.果断又删掉了,习惯真可怕 ...
- HDU 4513 吉哥系列故事――完美队形II(Manacher)
题目链接:cid=70325#problem/V">[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher V - 吉哥系列故事――完美队形I ...
- HDU 4513 吉哥系列故事——完美队形II manacher
吉哥系列故事——完美队形II Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希 ...
- hdu----(4513)吉哥系列故事——完美队形II(manacher(最长回文串算法))
吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- hdu4513完美队形II manacher
吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要 ...
- HDU4513 吉哥系列故事——完美队形II Manacher算法
题目链接:https://vjudge.net/problem/HDU-4513 吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others) Me ...
- V - 吉哥系列故事――完美队形I Manacher
吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要 ...
- HDU4513:完美队形II(Manacher)
Description Input Output Sample Input Sample Output Solution 才发现我之前不会证$Manacher$复杂度……QAQ 题意是求最长向 ...
- HDU 4513 吉哥系列故事——完美队形II (Manacher变形)
题意:假设有n个人按顺序的身高分别是h[1], h[2] ... h[n],从中挑出一些人形成一个新的队形,新的队形若满足以下要求,则就是新的完美队形: 1.连续的 2.形成回文串 3.从左到中间那 ...
随机推荐
- kettle转换设置变量,校验输出新变量
背景:有很多小的转换需要串联起来,如果前一个执行成功,后面继续接着执行,如果执行等待中,就让程序等一会再次获取数据分析,如果失败就中止,成功就进行下一个转换,以此类推.... 需求:通过job把参数传 ...
- c++11特性
0. 简介 在c++11标准中, 语言本身和标准库都增加了很多新内容. 里面的某些特性, 会让你在代码编写时更优雅. 我的环境: 系统: ubuntu16.04 g++版本: g++5.4.0 使用c ...
- Java 多线程创建和线程状态
一.进程和线程 多任务操作系统中,每个运行的任务是操作系统运行的独立程序. 为什么引进进程的概念? 为了使得程序能并发执行,并对并发执行的程序加以描述和控制. 因为通常的程序不能并发执行,为使程序(含 ...
- Tomcat 的部署器
要使用一个Web应用程序,必须要将表示该应用程序的Context实例部署到一个Host实例中,在Tomcat中,Context实例可以用WAR文件的形式来部署,也可以将整个WEB应用程序复制到Tomc ...
- 解决MyEclipse发布按钮无效的办法
删除Workspaces目录(存放您MyEclipse项目的地方)下的 “/.metadata/.plugins/org.eclipse.core.runtime/.settings/com.genu ...
- webmagic学习之路-2:采集安居客经纪人列表
相比较 1 稍微成熟了一点,会用的东西多了. 正则用的不好,很多东西不会,大神轻喷! package com.action; import java.util.ArrayList; import ja ...
- C语言无法使用引用,一定要使用怎么办? ------指针的指针做参数
#include <stdio.h> #include <stdlib.h> #include <string.h> void fun1(char** s); vo ...
- Boost Graph Library materials
Needed to compute max flow in a project and found the official document of BGL to be rather obscure, ...
- 如何使用Jedis操作redis
public class JredisTest { private static Jedis jedis = new Jedis("localhost", 6379); publi ...
- python入坑级
pycharm设置 pycharm设置自动换行的方法 只对当前文件有效的操作:菜单栏->View -> Active Editor -> Use Soft Wraps: 如果想对所有 ...