HDU 4745 Two Rabbits(最长回文子序列)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Description
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
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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = ; 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] = + (a[l] == a[r]);
if(a[l] == a[r]) dp[l][r] = dfs(pre[l], next[r]) + ;
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 = ; i < n; ++i) scanf("%d", &a[i]);
if(n < ) printf("%d\n", n);
else {
memset(dp, , sizeof(dp));
for(int i = ; i < n; ++i)
pre[i] = (i - + n) % n, next[i] = (i + ) % n, dp[i][i] = ;
int ans = ;
for(int i = ; i < n; ++i)
ans = max(ans, max(dfs(pre[i], i), dfs(pre[i], next[i]) + ));
printf("%d\n", ans);
}
}
}
HDU 4745 Two Rabbits(最长回文子序列)(2013 ACM/ICPC Asia Regional Hangzhou Online)的更多相关文章
- HDU 4745 Two Rabbits ★(最长回文子序列:区间DP)
题意 在一个圆环串中找一个最长的子序列,并且这个子序列是轴对称的. 思路 从对称轴上一点出发,向两个方向运动可以正好满足题意,并且可以证明如果抽选择的子环不是对称的话,其一定不是最长的. 倍长原序列, ...
- HDU4745——Two Rabbits——2013 ACM/ICPC Asia Regional Hangzhou Online
这个题目虽然在比赛的时候苦思无果,但是赛后再做就真的是个水题,赤果果的水题. 题目的意思是给n个数构成的环,两只兔子从任一点开始分别顺逆时针跳,每次可以调到任意一个数(最多不会跳过一圈). 求最多能跳 ...
- hdu 4747 Mex (2013 ACM/ICPC Asia Regional Hangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 思路: 比赛打得太菜了,不想写....线段树莽一下 实现代码: #include<iost ...
- [2013 ACM/ICPC Asia Regional Hangzhou Online J/1010]hdu 4747 Mex (线段树)
题意: + ;];;;], seg[rt << | ]);)) * fa.setv;) * fa.setv;;], seg[rt << | ], r - l + );;, ...
- HDU 4744 Starloop System(最小费用最大流)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Description At the end of the 200013 th year of the Galaxy era, the war between Carbon-based lives a ...
- HDU 4747 Mex(线段树)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Problem Description Mex is a function on a set of integers, which is universally used for impartial ...
- HDU 4717 The Moving Points(三分法)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description There are N points in total. Every point moves in certain direction and certain speed. W ...
- HDU 4722 Good Numbers(位数DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description If we sum up every digit of a number and the result can be exactly divided by 10, we say ...
- HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)
Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...
随机推荐
- 将某页面中ajax中获取到的信息放置到sessionStorage中保存,并在其他页面调用这些数据。
A页面代码: var obj = data.data; var infostr = JSON.stringify(obj);//转换json sessionStorage.obj = infostr; ...
- Python学习之路——基础2(含深浅拷贝)
逻辑运算符:not and or 等同于c/c++中的 !.&&.||,除了写法上的不同,实际原理是一样的. 运算也遵循短路原则.由于Python本身不支持++/--操作符,所以避 ...
- iOS | 地图定位
在IOS开发中,最常见的功能之一就是地图定位功能,不单单是百度地图,高德地图等专业的地图导航软件,还有美团,咕咚等一些美食购物类和运动类也需要这样的功能,所以学会这项技能是一名IOS开发工程师必须的. ...
- djano-模板层基础知识
########模板层######## 模板层其实就是templates文件夹里的html文件 其实这里的每个html不是真正意义的上html代码,只有经过模板渲染过后才算的上真正的html页面. 一 ...
- ABAP术语-Database Rollback
Database Rollback 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/24/1051238.html Operation tha ...
- Java语言利用Collections.sort对Map,List排序
1.main方法包含TreeMap排序1,TreeMap排序2,HashMap排序,List<Integer>排序,List<Bean>排序,List<Map>排序 ...
- JavaScript--文本框中只允许输入数字的操作(其他字符不显示)
在web网页中,尤其是某些提交表单操作,需要验证文本框输入内容,本文利用文本框键盘事件和事件对象,对文本框只允许输入数字方法进行总结. 1.键盘事件 keydown ---->键盘按下事件 ...
- ctf题目writeup(6)
2019.2.2 依旧是bugku上面的题目,地址:https://ctf.bugku.com/challenges 1. 解压后是60多个out.zip,都是真加密,里面都是1kb的data.txt ...
- 一个新晋IT行业的努力Duiker
亲爱的朋友,你好! 我很开心能以这么一篇博客来开始我的IT努力之路.我叫Duiker,是一名软件工程专业的学生,想通过写博客来提升自己,充实自我. 首先,我要确立自己的学习编程目标: 1.将算 ...
- javaweb 项目部署到tomcat
1.准备环境 1.安装 mysql 2.安装java 环境 3.使用idea打war包 点击 apply 生成war包 将war包传到tomcat的webapp目录下,进入tomcat安装目录,进入b ...