您的位置:首页 > 其它

HDU 4745 Two Rabbits(最长回文子序列)(2013 ACM/ICPC Asia Regional Hangzhou Online)

2013-09-16 19:38 453 查看
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 = 1010;

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] = 1 + (a[l] == a[r]);
if(a[l] == a[r]) dp[l][r] = dfs(pre[l], next[r]) + 2;
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 = 0; i < n; ++i) scanf("%d", &a[i]);
if(n < 3) printf("%d\n", n);
else {
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; ++i)
pre[i] = (i - 1 + n) % n, next[i] = (i + 1) % n, dp[i][i] = 1;
int ans = 0;
for(int i = 0; i < n; ++i)
ans = max(ans, max(dfs(pre[i], i), dfs(pre[i], next[i]) + 1));
printf("%d\n", ans);
}
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐