Description

Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon, they planned to play a game with some stones. There were n stones on the ground and they were arranged as a clockwise ring. That is to say, the first stone was adjacent to the second stone and the n-th stone, and the second stone is adjacent to the first stone and the third stone, and so on. The weight of the i-th stone is ai.

The rabbits jumped from one stone to another. Tom always jumped clockwise, and Jerry always jumped anticlockwise.

At the beginning, the rabbits both choose a stone and stand on it. Then at each turn, Tom should choose a stone which have not been stepped by itself and then jumped to it, and Jerry should do the same thing as Tom, but the jumping direction is anti-clockwise.

For some unknown reason, at any time , the weight of the two stones on which the two rabbits stood should be equal. Besides, any rabbit couldn't jump over a stone which have been stepped by itself. In other words, if the Tom had stood on the second stone, it cannot jump from the first stone to the third stone or from the n-the stone to the 4-th stone.

Please note that during the whole process, it was OK for the two rabbits to stand on a same stone at the same time.

Now they want to find out the maximum turns they can play if they follow the optimal strategy.

 

Input

The input contains at most 20 test cases. 
For each test cases, the first line contains a integer n denoting the number of stones. 
The next line contains n integers separated by space, and the i-th integer ai denotes the weight of the i-th stone.(1 <= n <= 1000, 1 <= ai <= 1000) 
The input ends with n = 0.
 

Output

For each test case, print a integer denoting the maximum turns.
 
题目大意:有一个环n个点,每个点有一个权值,两只兔子各自从任意点出发,一只顺时针跳,一只逆时针条,任意时刻两只兔子只能落在权值相同的点上,两只兔子都只能跳一个圈,问最多能跳多少下。
思路:用dp[l][r]表示,一只兔子从 l 开始顺时针跳,另一只兔子从 r 开始逆时针跳,直到兔子 r 跳到 l,兔子 l 跳到 r,最多能跳多少步。
然后,可以发现出发点必然是相同或者相邻(最优解肯定会相遇一次,相遇的时候只可能是a[i]和a[i+1]相同,互相跳过,或者落在同一个点,我们可以把那个最优解的起点挪到这里)
那么ans = max(dp[i-1][i+1]+1, dp[i][i+1]), i ∈ [0, n - 1]
 
代码(125MS):
 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = ; int dp[MAXN][MAXN], a[MAXN];
int pre[MAXN], next[MAXN];
int n; int dfs(int l, int r) {
if(dp[l][r]) return dp[l][r];
if(pre[l] == r) return dp[l][r] = + (a[l] == a[r]);
if(a[l] == a[r]) dp[l][r] = dfs(pre[l], next[r]) + ;
else dp[l][r] = max(dfs(pre[l], r), dfs(l, next[r]));
return dp[l][r];
} int main() {
while(scanf("%d", &n) != EOF && n) {
for(int i = ; i < n; ++i) scanf("%d", &a[i]);
if(n < ) printf("%d\n", n);
else {
memset(dp, , sizeof(dp));
for(int i = ; i < n; ++i)
pre[i] = (i - + n) % n, next[i] = (i + ) % n, dp[i][i] = ;
int ans = ;
for(int i = ; i < n; ++i)
ans = max(ans, max(dfs(pre[i], i), dfs(pre[i], next[i]) + ));
printf("%d\n", ans);
}
}
}

HDU 4745 Two Rabbits(最长回文子序列)(2013 ACM/ICPC Asia Regional Hangzhou Online)的更多相关文章

  1. HDU 4745 Two Rabbits ★(最长回文子序列:区间DP)

    题意 在一个圆环串中找一个最长的子序列,并且这个子序列是轴对称的. 思路 从对称轴上一点出发,向两个方向运动可以正好满足题意,并且可以证明如果抽选择的子环不是对称的话,其一定不是最长的. 倍长原序列, ...

  2. HDU4745——Two Rabbits——2013 ACM/ICPC Asia Regional Hangzhou Online

    这个题目虽然在比赛的时候苦思无果,但是赛后再做就真的是个水题,赤果果的水题. 题目的意思是给n个数构成的环,两只兔子从任一点开始分别顺逆时针跳,每次可以调到任意一个数(最多不会跳过一圈). 求最多能跳 ...

  3. hdu 4747 Mex (2013 ACM/ICPC Asia Regional Hangzhou Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 思路: 比赛打得太菜了,不想写....线段树莽一下 实现代码: #include<iost ...

  4. [2013 ACM/ICPC Asia Regional Hangzhou Online J/1010]hdu 4747 Mex (线段树)

    题意: + ;];;;], seg[rt <<  | ]);)) * fa.setv;) * fa.setv;;], seg[rt <<  | ], r - l + );;,  ...

  5. HDU 4744 Starloop System(最小费用最大流)(2013 ACM/ICPC Asia Regional Hangzhou Online)

    Description At the end of the 200013 th year of the Galaxy era, the war between Carbon-based lives a ...

  6. HDU 4747 Mex(线段树)(2013 ACM/ICPC Asia Regional Hangzhou Online)

    Problem Description Mex is a function on a set of integers, which is universally used for impartial ...

  7. HDU 4717 The Moving Points(三分法)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description There are N points in total. Every point moves in certain direction and certain speed. W ...

  8. HDU 4722 Good Numbers(位数DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description If we sum up every digit of a number and the result can be exactly divided by 10, we say ...

  9. HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)

    Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...

随机推荐

  1. AngularJS显示一个简单表格

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. git简单配置

    1.安装完git查看版本 git --version 2.配置用户名邮箱 git config --global user.name "chencheng" git config ...

  3. jzoj5195. 【NOIP2017提高组模拟7.3】A(递推,打表)

    Description

  4. mysql查看锁等信息SQL

    查看锁等信息,包括锁信息: select "HOLD:",ph.id h_processid,trh.trx_id h_trx_id,trh.trx_started h_start ...

  5. Docker运行Nginx服务器

    一.获取Docker容器的Nginx镜像 二.创建Docker容器宿主机挂载目录 # 创建挂载目录,-v 显示创建的目录名 [root@idclooknet ~]# mkdir -vp /opt/do ...

  6. 服务器空间不足导致mysql服务器无法运行

    今天有朋友请我帮忙解决一个问题,他公司服务器mysql数据库一直连接失败.登录服务期之后发现服务器空间占满了,导致mysql不能启动. 下面说解决方法: 首先查看空间占用,发现空间占满了 df -h ...

  7. Java线程:概念与使用

    Java线程大总结 原文章地址:一篇很老的专栏,但是现在看起来也感觉深受启发,知识点很多,很多线程特点我没有看,尴尬.但是还是整理了一下排版,转载一下. 操作系统中线程和进程的概念 在现代操作系统中, ...

  8. java程序——两数的加减乘除

    import javax.swing.JOptionPane; // import class JOptionPane public class Elementary { public static ...

  9. Hibernate-ORM:16.Hibernate中的二级缓存Ehcache的配置

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述Hibernate中的二级缓存的配置,作者将使用的是ehcache缓存 一,目录 1.二级缓存的具 ...

  10. Linq工具篇(1)——使用LinqPad

    学习Linq,有一个非常强大的工具,那就是LinqPad,具体功能有多强大就不说了,网上百度一下就可以知道,百闻不如一见,用用就知道,在网上下载一个绿色版的,无需安装,直接运行,界面如下: 具体功能, ...