• HDU 4745 Two Rabbits


    Two Rabbits

    Time Limit: 5000ms
    Memory Limit: 65535KB
    This problem will be judged on HDU. Original ID: 4745
    64-bit integer IO format: %I64d      Java class name: Main
     
    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

     
    解题:其实求的是最长回文子序列
     
    一个兔子不能经过一个位置多次,两个兔子的走的路线是可以交叉的
     
    dp[i][j]表示从i到j形成的最长回文子串
    最后要处理环形的 我们可以分 dp[1][i] 与 dp[i+1][n]表示一只兔子有i跳到1再跳到n最后停在i+1 
    另一只兔子由i+1跳到n又跳到1最后停在i
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 1010;
     4 int dp[maxn][maxn],a[maxn],n;
     5 int main() {
     6     while(scanf("%d",&n),n) {
     7         memset(dp,0,sizeof dp);
     8         for(int i = 1; i <= n; ++i) {
     9             scanf("%d",a + i);
    10             dp[i][i] = 1;
    11         }
    12         for(int i = n; i > 0; --i) {
    13             for(int j = i + 1; j <= n; ++j) {
    14                 dp[i][j] = max(dp[i][j],max(dp[i][j-1],dp[i+1][j]));
    15                 if(a[i] == a[j]) dp[i][j] = max(dp[i][j],dp[i+1][j-1] + 2);
    16             }
    17         }
    18         int ret = 0;
    19         for(int i = 1; i <= n; ++i)
    20             ret = max(ret,dp[1][i] + dp[i+1][n]);
    21         printf("%d
    ",ret);
    22     }
    23     return 0;
    24 }
    View Code
  • 相关阅读:
    Python数据可视化的四种简易方法
    阿里如何将“高峰前扩容、高峰后缩容”的梦想照进现实?
    支付宝客户端架构解析:Android 容器化框架初探
    阿里工业互联网平台“思考”:一场从0到1的蜕变
    关于Flutter初始化流程,我必须告诉你的是...
    类在什么时候加载和初始化
    instanceof 与isAssignableFrom
    Java注释@interface的用法【转】
    网站过滤器Filter
    正则表达式
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4786282.html
Copyright © 2020-2023  润新知