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. Element.getBoundingClientRect()

    Element.getBoundingClientRect()方法会返回元素的大小和相对于视口的位置 语法: var domRect = element.getBoundingClientRect() ...

  2. 19.springboot邮件服务服务器部署访问不到邮箱服务器解决方案

    1.前言 在Springboot项目的生产环境中,win系统环境下,邮箱服务是可以正常使用的. 当项目部署到阿里云服务器上之后,因为服务器端口采用安全组的方式,25端口访问不到. 在网上查找了一部分资 ...

  3. bootstrap到底是用来做什么的

    Bootstrap官网:http://v3.bootcss.com/ Bootstrap是Twitter推出的一个用于前端开发的开源工具包.它由Twitter的设计师Mark Otto和Jacob T ...

  4. 原生js的常见封装

    )); } ;;;;]){                 ];                 ] = ;;;,)     ,)     ,)     ,)         ,)         , ...

  5. sudo及visudo

    sudo是一种权限管理机制,管理员可以授权普通用户去执行root的操作,而不需要知道root的密码,它依赖于/etc/sudoers这个文件,可以授权给哪个用户在哪个主机上能够以管理员的身份执行什么样 ...

  6. hibernate中配置单向多对一关联,和双向一对多,双向多对多

    什么是一对多,多对一? 一对多,比如你去找一个父亲的所有孩子,孩子可能有两个,三个甚至四个孩子. 这就是一对多 父亲是1 孩子是多 多对一,比如你到了两个孩子,它们都是有一个共同的父亲. 此时孩子就是 ...

  7. [NodeJs系列][译]理解NodeJs中的Event Loop、Timers以及process.nextTick()

    译者注: 为什么要翻译?其实在翻译这篇文章前,笔者有Google了一下中文翻译,看的不是很明白,所以才有自己翻译的打算,当然能力有限,文中或有错漏,欢迎指正. 文末会有几个小问题,大家不妨一起思考一下 ...

  8. 【ISIS(中间系统到中间系统)路由链路状态信息协议初识】

    ISIS单区域的基本配置 一:根据项目需求,考虑到组网的规模和条件,部署ISIS单区域的拓扑图如下: 二:配置 1:首先对RTA进行配置,在系统视图创建ISIS进程:进入ISIS配置视图,指定IS的级 ...

  9. css公共类

    /*iOS弹性滚动*/ .scrolling{ position: absolute; width: 100%; height:100%; overflow-x:hidden; overflow-y: ...

  10. C# WebClient 使用http免费代理

    static void Main(string[] args) { WebClient client = new WebClient(); client.Encoding = Encoding.Get ...