HDOJ 4745 Two Rabbits DP

Two Rabbits

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 944    Accepted Submission(s): 496

Problem 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.

Sample Input

1
1
4
1 1 2 1
6
2 1 1 2 1 3
0

Sample Output

1
4
5

Hint

For the second case, the path of the Tom is 1, 2, 3, 4, and the path of Jerry is 1, 4, 3, 2.
For the third case, the path of Tom is 1,2,3,4,5 and the path of Jerry is 4,3,2,1,5.

Source

2013 ACM/ICPC Asia Regional Hangzhou Online

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=2200;

int n;
int a[maxn];
int dp[maxn][maxn];

int main()
{
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",a+i);
            a[n+i]=a[i];
        }
        memset(dp,0,sizeof(dp));
        n+=n;
        for(int i=0;i<n;i++)
        {
            dp[i][i]=1;
        }
        for(int len=2;len<n;len++)
        {
            for(int i=0;i+len-1<n;i++)
            {
                int j=i+len-1;
                dp[i][j]=max(dp[i][j],max(dp[i+1][j],dp[i][j-1]));
                if(a[i]==a[j])
                {
                    dp[i][j]=max(dp[i][j],dp[i+1][j-1]+2);
                }
            }
        }
        int ans=1;
        for(int i=0;i+n/2-1<n;i++)
        {
            ans=max(ans,dp[i][i+n/2-1]);
        }
        for(int i=0;i+n/2-2<n;i++)
        {
            ans=max(ans,dp[i][i+n/2-2]+1);
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-11-07 10:41:55

HDOJ 4745 Two Rabbits DP的相关文章

LPS HDOJ 4745 Two Rabbits

题目传送门 1 /* 2 题意:一只兔子顺时针跳,另一只逆时针跳,跳石头权值相等而且不能越过起点 3 LPS:这道就是LPS的应用,把环倍增成链,套一下LPS,然而并不能理解dp[i][i+n-2] + 1,看别人的解题报告吧,以后来补(玩游戏) 4 详细解释 5 */ 6 /************************************************ 7 * Author :Running_Time 8 * Created Time :2015-8-8 16:57:23 9

HDU 4745 Two Rabbits(DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4745 题意:n个数排成一个环.两个人AB初始时各自选定一个位置.每一轮A在顺时针方向选择一个位置,B在逆时针选择一个位置,且这两个人所选位置的数字相等,然后格子跳到新选的位置上.问最多进行多少轮?有一个限制为每次跳跃不能跨过以前自己曾经选过的格子. 思路:主要是分析问题的本质.其实就是求最长回文子列.f[i][j]为[i,j]的最长回文子列,则答案为max(f[1][i],f[i+1][n]). i

模拟 HDOJ 4552 Running Rabbits

题目传送门 1 /* 2 模拟:看懂题意,主要是碰壁后的转向,笔误2次 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <vector> 8 using namespace std; 9 10 const int MAXN = 1e3 + 10; 11 const int INF = 0x3f3f3f3f; 12 struct Rabbit 13

HDU 4745 Two Rabbits【非连续最长回文子序列,区间DP】

SubmitStatus 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

HDU 4745 Two Rabbits (区间DP)

题意: 两只兔子,在一个由n块石头围成的环上跳跃,每块石头有一个权值ai.开始时两兔站在同一石头上(也算跳1次),一只从左往右跳,一只从右往左跳,两只同时跳,而每跳一次,两只兔子所站的石头的权值都要相等,在一圈内(各自不能越过起点,也不能再次回到起点)它们(单只兔子)最多能跳多少次(1 <= n <= 1000, 1 <= ai <= 1000). 思路: 此题要求的就是最长回文子序列(并不是子串),而最长回文子序列的算法复杂度为O(n*n).但是由于是个环上,所以要挖掘一下环的性

HDU 4745 Two Rabbits (区间DP)

题意:给定一个圆形的环,有两个只兔子,一只顺时针跳,一个逆时针,但每次跳到的石头必须一样,问你最多能跳多少轮. 析:本来以为是LCS呢,把那个序列看成一个回文,然后就能做了,但是时间受不了.其实是一个区间DP,dp[i[j] 表示从 i 到 j 中最长的回文数. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #in

HDU 4745 Two Rabbits(最长回文子序列)

http://acm.hdu.edu.cn/showproblem.php?pid=4745 题意: 有一个环,现在有两只兔子各从一个点开始起跳,一个沿顺时针,另一个沿逆时针,只能在一圈之内跳,并且每次所在的点的大小必须相同,问最多能经过 几个点. 思路:环状的话可以先倍增改成链. 这道题目的话就是求最长回文子串,它的求法是这样的: 设字符串为str,长度为n,p[i][j]表示第i到第j个字符间的子序列的个数(i<=j),则: 状态初始条件:dp[i][i]=1 (i=0:n-1) 状态转移方

HDOJ 4705 Y 树形DP

DP:求出3点构成链的方案数 ,然后总方案数减去它 Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1738    Accepted Submission(s): 493 Problem Description Sample Input 4 1 2 1 3 1 4 Sample Output 1 Hint 1. The only

HDOJ 1003 Max Sum(dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 思路分析:该问题为最大连续子段和问题,使用动态规划求解: 1)最优子结构:假设数组为A[0, 1, 2,….., n],在所有的可能的解中,即解空间中找出所有的解,可以知道,所有的解都为以A[j](j = 0, 1, …, n) 为尾的连续子段,则假设dp[j]表示以在数组A[1, 2, …, j]中以A[j]结尾的字段的最大的和,我们就可以刻画子空间中的所有解的特征:如果 dp[j] > 0