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 ...
随机推荐
- tracking
https://reid-mct.github.io/ 1st Workshop on Target Re-Identification and Multi-Target Multi-Camera ...
- vue2.0移除或更改的一些东西
一.vue2.0移除了$index和$key 虽然说现在很多文章说他们的代码是vue2.0版本的,但是有一些仔细一看,发现并不全是2.0版本,有些语法还是1.0的版本,比如这个$index,$key, ...
- 8.Element-ui日期组件上传到后台日期少一天解决办法
<el-date-picker type="date" value-format="yyyy-MM-dd" placeholder="转出日期& ...
- 菜鸟笔记 -- Chapter 6.4.2 详解继承
6.4.2 详解继承 6.4.2.1 继承入门 继承使得程序架构具有一定的弹性,在程序中复用一些已经定义完善的类不仅可以减少软件开发周期,也可以提高软件的可维护性和可扩展性.基本思想是基于某个父类 ...
- mysql快速导入导出数据
--导入 select * from inhos_genoperation(表名) where UPLOAD_ORG_CODE='***' into outfile '/tmp/inhos_genop ...
- flask中的request
1.request是什么? 简单来说,它就是flask的封装的一个对象,这个对象包含着前端请求所带的所有信息.既然说它是一个对象,那么它肯定是有一些熟悉,和方法的,下面就来介绍下request里的熟悉 ...
- ETO的公开赛T3《寻星》 题解(BY 超級·考場WA怪 )
题解 寻星 题意:给定一个有向带权图,定义从一点到另一点的某条路径长为路径上所有边权的最大值,并给定四个点编号w,t1,t2,t3. 求出一个点s,使它在到t1,t2,t3三点最短路径最大值最大或者根 ...
- BZOJ1086: [SCOI2005]王室联邦(贪心,分块?)
Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 2610 Solved: 1584[Submit][Status] ...
- 字符串拼接在Oracle和mysql中的用法
oracle拼接字符串 1.使用 '||' 或者 concat(参数1,参数2) select 'aa' || 'bb' || 'cc' from dual; 结果:aabbcc select co ...
- 洛谷 U45568 赌神:决斗
题目描述 \mathcal{tomoo}tomoo决定与\mathcal{CYJian}CYJian进行决斗! 已知\mathcal{tomoo}tomoo有\mathcal{N}N张扑克牌,每张扑克 ...