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
 

#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;
}

版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss

HDOJ 4745 Two Rabbits DP的更多相关文章

  1. LPS HDOJ 4745 Two Rabbits

    题目传送门 /* 题意:一只兔子顺时针跳,另一只逆时针跳,跳石头权值相等而且不能越过起点 LPS:这道就是LPS的应用,把环倍增成链,套一下LPS,然而并不能理解dp[i][i+n-2] + 1,看别 ...

  2. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  3. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

  4. HDOJ(HDU).2159 FATE (DP 带个数限制的完全背包)

    HDOJ(HDU).2159 FATE (DP 带个数限制的完全背包) 题意分析 与普通的完全背包大同小异,区别就在于多了一个个数限制,那么在普通的完全背包的基础上,增加一维,表示个数.同时for循环 ...

  5. HDOJ(HDU).1114 Piggy-Bank (DP 完全背包)

    HDOJ(HDU).1114 Piggy-Bank (DP 完全背包) 题意分析 裸的完全背包 代码总览 #include <iostream> #include <cstdio&g ...

  6. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  7. 模拟 HDOJ 4552 Running Rabbits

    题目传送门 /* 模拟:看懂题意,主要是碰壁后的转向,笔误2次 */ #include <cstdio> #include <algorithm> #include <c ...

  8. HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)

    Two Rabbits Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total ...

  9. hdu 4745 Two Rabbits 区间DP

    http://acm.hdu.edu.cn/showproblem.php?pid=4745 题意: 有两只兔子Tom Jerry, 他们在一个用石头围城的环形的路上跳, Tom只能顺时针跳,Jerr ...

随机推荐

  1. Windows phone 8 学习笔记(5) 图块与通知

    原文:Windows phone 8 学习笔记(5) 图块与通知 基于metro风格的Windows phone 8 应用提到了图块的概念,它就是指启动菜单中的快速启动图标.一般一个应用必须有一个默认 ...

  2. OCP读书笔记(17) - 计划任务

    轻量级作业: 也称为持久性轻量级作业,如果当我们的数据库每秒钟需要创建.删除或修改数十个或数百个作业时,使用轻量级作业是降低开销的最佳方法 常规作业:是由oracle 11g Scheduler 所支 ...

  3. 理解RESTful架构(转)

    越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(high latency).高 ...

  4. hibernate 批量处理数据

    批量处理数据是指处理大量数据的一个单独的事务. 在应用层批处理操作, 主要有以下方式: 通过 Session 通过 HQL 通过 StatelessSession 通过 JDBC API(仅仅要会用这 ...

  5. CSS检测的高像素密度屏幕设备

    iPhone4尽管是640px解析度,但它的屏幕宽度(device-width)目前只有320px和iPhone3G相同.只是iPhone4S的像素密度2. 然后使用meta viewport什么时候 ...

  6. 解决wordpress发表文章,照片不能居中的问题

    最近,随着一个年轻漂亮的女人的帮助(简直美极了.图相当火爆,性格非常好.大家闺秀型,照片给大家看看下一个突发.哈哈)获取她的个人博客,地址似乎是www.okaaok.com遇到发表文章.照片不能反正水 ...

  7. Android规范发展

    一.Android 编码规范 1.java 代码中不出现中文.最多凝视中能够出现中文 2.局部变量命名.静态成员变量命名 仅仅能包括字母,单词首字母出第一个外,都为大写,其它字母都为小写 3.常量命名 ...

  8. java Socket的怪异之处

    怪异之一: connect(SocketAddress endpoint):这个方法,尝试连接server端,如果连接不上,就抛出IOException异常.如果连接成功了,就继续执行下一步的代码. ...

  9. lua学习笔记11:lua中的小技巧

    lua中的小技巧,即基础lua语言本身的特种,进行一个些简化的操作 一. 巧用or x = x or v 等价于: if not x then x = v end 假设x为nil或false,就给他赋 ...

  10. 转: 第二章 IoC Annotation注入

    http://blog.csdn.net/p_3er/article/details/9231307 1.命名空间 使用Annotation的方式,需要在spring的配置文件中配置命名空间.命名空间 ...