zoukankan      html  css  js  c++  java
  • hdu 4745 Two Rabbits 区间dp

    Two Rabbits

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 2845    Accepted Submission(s): 1484


    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.
     
    题意:给n个环状的数,A、B两个兔子沿相反的方向走,每次走一步,要求两个兔子每次到达的数要相同,(限制在一圈的范围内),问兔子最远能走几步。
     
    题解:
    首先,把环倍增改成链,接下来找符合题意的最大步数
    用dp[i][j]表示在区间[i,j]内,兔子能走的最大步数
     
    初始化dp,预处理问题的子结构,如果a[i]==a[j] ,  dp[i][j]=dp[i+1][j-1]+2
    如果 a[i]!=a[j]  ,  dp[i][j]=max(dp[i+1][j],dp[i][j-1])
     
    最后输出最大步数,这里要考虑两种情况:
    因为题目要求是:两个兔子往反方向走,并且要求范围限制在一圈之内。
    1、如果两个兔子的开始起点不相同,那么每个兔子由起点i能走的最远位置为i+n-1;所以最后的结果是枚举dp[i][i+n-1]的最大值
    2、如果开始起点相同,那么第一个起点肯定符合题目要求,跳过这个点直接从下一个点开始判断,最后的结果是枚举dp[i][i+n-2]+1的最大值
     
     
    #include<iostream>
    #include<string.h>
    #include<string>
    #include<math.h>
    #define ll long long
    #define mod 10007
    using namespace std;
    int n,t;
    int dp[2005][2005];//dp[i][j]表示在区间i到j内,兔子能走的最大步数
    int a[2005];
    ll max(ll a,ll b)
    {
        return a<b?b:a;
    }
    
    int main()
    {
        while(cin>>n)
        {
            if(n==0)
                break;
            for(int i=1;i<=n;i++)
            {
                cin>>a[i];
                a[i+n]=a[i];//把环倍增改成链
                dp[i][i]=1;
                dp[n+1][n+1]=1;
            }
            for(int len=2;len<=n;len++)//区间DP
            {
                for(int i=1;i+len-1<=2*n;i++)
                {
                    int j=i+len-1;
                    if(a[i]==a[j])
                        dp[i][j]=dp[i+1][j-1]+2;
                    else
                        dp[i][j]=max(dp[i+1][j],dp[i][j-1]);
                }
            }
            int ans=0;
            for(int i=1;i<=n;i++)//判断兔子是从同一起点走步数大,还是从不同起点开始步数大
            {
                ans=max(ans,dp[i][i+n-1]);
                ans=max(ans,dp[i][i+n-2]+1); //两只兔子从同一个起点出发
            }
            
            cout<<ans<<endl;
        }
        return 0;
    
    }
  • 相关阅读:
    用户界面线程(含有消息泵的线程)主线程与用户界面线程的通信
    poj 2151 Check the difficulty of problems 概率dp
    Codeforces 148D Bag of mice 概率dp
    [置顶] UVa在线比赛单题汇总DP专题
    UVa 10405 Longest Common Subsequence 最长公共子序列模板
    UVa 12018 Juice Extractor 切水果dp暂时存疑
    UVa 674 Coin Change 背包dp
    hdu 3853 LOOPS 圆环之理 概率dp
    UVa 111 History Grading 最长递增子序列 LIS
    UVa在线比赛单题汇总DP专题
  • 原文地址:https://www.cnblogs.com/-citywall123/p/10922506.html
Copyright © 2011-2022 走看看