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. Xtrabackup备份与恢复MySQL

    1.innobackupex备份原理 .innobackupex启动并fork一个进程启动xtrabackup,然后等待xtrabackup备份InnoDB文件; .xtrabackup备份时存在两个 ...

  2. HTML5新标签的兼容性处理

    普通浏览器 普通不支持HTML5新标签的浏览器 -- 能正常解析,但会当初成 inline 元素对待 在不支持HTML5新标签的浏览器里,会将这些新的标签解析成行内元素(inline)对待,所以我们只 ...

  3. react.js中实现tab吸顶效果问题

    在react项目开发中有一个需求是,页面滚动到tab所在位置时,tab要固定在顶部. 实现的思路其实很简单,就是判断当滚动距离scrollTop大于tab距离页面顶部距离offsetTop时,将tab ...

  4. SP1043 GSS1 - Can you answer these queries I(线段树,区间最大子段和(静态))

    题目描述 给出了序列A[1],A[2],…,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y} ...

  5. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--L-用来作弊的药水

    链接:https://www.nowcoder.com/acm/contest/90/L 来源:牛客网 1.题目描述 -- 在一个风雨交加的夜晚,来自异世界的不愿透露姓名的TMK同学获得了两种超强药水 ...

  6. ABAP术语-ISO (International Organization for Standardization)

    ISO (International Organization for Standardization) 原文:http://www.cnblogs.com/qiangsheng/archive/20 ...

  7. 右键添加git-bash

    主要: 右键如果没有git-bash,如何给右键手动添加 前面对右键存在git-bash但使用出现问题的解决,也想到如果右键都没有,该如何给右键添加了,于是接着记录下如何添加的过程: 情形: 手动给右 ...

  8. Fibonacci使用递归和循环实现

    #include<stdio.h> double Fibonacci(int i); double Fibonacci_(int i); int main(void) { int i; p ...

  9. golang 三个点的用法

    已经忘了这是第几次查这个用法了,还是记一下吧~ ^ _ ^ 本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/137 ...

  10. Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String

    题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...