区间dp-hdu-4745-Two Rabbits
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4745
题目意思:
给n个环状的数,A、B两人沿相反的方向走,每单位时间走一步,要求相同时间两人到达相同的数,且同一位置同一个人不能走两次,走过的位置不能越过。
解题思路:
根据回文非连续序列的性质,从前往后,和从后往前序列是一样的,所以只用求出区间内最长的回文序列即可,又由于是环状,所以分成两部分,1~i i+1~n,A可以从i走到1,然后从n走到i+1,B可以从1走到i,从i+1走到n .
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#define eps 1e-6
#define INF 0x3fffffff
#define PI acos(-1.0)
#define ll __int64
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define Maxn 1100
int sa[Maxn];
int dp[Maxn][Maxn]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n; while(scanf("%d",&n)&&n)
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
scanf("%d",&sa[i]);
dp[i][i]=1;
}
for(int i=2;i<=n;i++)
{
for(int j=1;j+i-1<=n;j++)
{
int k=i+j-1;
dp[j][k]=max(dp[j][k],max(dp[j+1][k],dp[j][k-1]));
if(sa[j]==sa[k])
dp[j][k]=max(dp[j][k],dp[j+1][k-1]+2);
}
}
int ans=0;
for(int i=1;i<=n;i++)
ans=max(ans,dp[1][i]+dp[i+1][n]);
printf("%d\n",ans); }
return 0;
}
区间dp-hdu-4745-Two Rabbits的更多相关文章
- hdu 4745 Two Rabbits 区间DP
http://acm.hdu.edu.cn/showproblem.php?pid=4745 题意: 有两只兔子Tom Jerry, 他们在一个用石头围城的环形的路上跳, Tom只能顺时针跳,Jerr ...
- HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)
Two Rabbits Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total ...
- 区间DP || HDU 6249 Alice’s Stamps
题意:标号为1-n的n种邮票,m个邮票集,每个集里有标号从Li到Ri的邮票,要从中选K个邮票集,使这K个邮票集能覆盖最多种的邮票,问最多能覆盖多少种邮票 思路:区间DP (我:??? f[i][j]表 ...
- HDU 4745 Two Rabbits ★(最长回文子序列:区间DP)
题意 在一个圆环串中找一个最长的子序列,并且这个子序列是轴对称的. 思路 从对称轴上一点出发,向两个方向运动可以正好满足题意,并且可以证明如果抽选择的子环不是对称的话,其一定不是最长的. 倍长原序列, ...
- HDU 4745 Two Rabbits (区间DP)
题意: 两只兔子,在一个由n块石头围成的环上跳跃,每块石头有一个权值ai.开始时两兔站在同一石头上(也算跳1次),一只从左往右跳,一只从右往左跳,两只同时跳,而每跳一次,两只兔子所站的石头的权值都要相 ...
- HDU 4745 Two Rabbits 区间dp_回文序列
题目链接: http://blog.csdn.net/scnu_jiechao/article/details/11759333 Two Rabbits Time Limit: 10000/5000 ...
- HDU 4745 Two Rabbits(最长回文子序列)
http://acm.hdu.edu.cn/showproblem.php?pid=4745 题意: 有一个环,现在有两只兔子各从一个点开始起跳,一个沿顺时针,另一个沿逆时针,只能在一圈之内跳,并且每 ...
- HDU 4745 Two Rabbits (2013杭州网络赛1008,最长回文子串)
Two Rabbits Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tota ...
- HDU 4745 Two Rabbits(最长回文子序列)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Description Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon ...
- 区间DP HDU 4283
t个数据 n个权值 1->n 可以入栈调整顺序 花费 第k个出来 w[i]*(k-1); 求花费最少 #include<stdio.h> #include<string.h&g ...
随机推荐
- 《how to design programs》第11章自然数
这章让我明白了原来自然数的定义本来就是个递归的过程. 我们通常用枚举的方式引出自然数的定义:0,1,2,3,等等(etc).最后的等等是什么意思?唯一能把等等从描述自然数的枚举方法中去除的方法是自引用 ...
- Implement Queue using Stacks 解答
Question Implement the following operations of a queue using stacks. push(x) -- Push element x to th ...
- What is NicEdit?
NicEdit - WYSIWYG Content Editor, Inline Rich Text Application What is NicEdit? NicEdit is a Light ...
- No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)
之前看内部类的时候没发现这个问题,今天写代码的时候遇到,写个最简单的例子: 下面这一段代码 红色的部分就是编译报错: No enclosing instance of type Outer is ac ...
- Segment(技巧 相乘转换成相加 + java)
Segment Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- STL中的set容器的一点总结(转)
STL中的set容器的一点总结 1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂 ...
- Cocos2d-x v3.0正式版尝鲜体验【1】 环境搭建和新建项目
Cocos2d-x v3.0在前天最终公布正式版了,等了大半年最终出来了.一直没去碰之前的3.0各种beta,rc版本号,就想等正式版出来再尝试. 昨天也參加了触控科技在成都举办的沙龙活动.看到作者王 ...
- Reflux 使用教程
Reflux是根据React的flux创建的单向数据流类库.Reflux的单向数据流模式主要由actions和stores组成.例如,当组件list新增item时,会调用actions的某个方法(如a ...
- 物理机与虚拟机IP互ping通,而互ping主机名不通
问题描述:虚拟机信息:VMware-workstation 10安装RHEL5.8操作系统.hostname:rhel201.com IP:192.168.1.201 物理机系统:windows 7主 ...
- 阿里云RDS导入服务器数据库 XtraBackup
如果是centos系统,默认会开启selinux 一定需关闭selinux 解决方法:关闭它,打开/etc/selinux/config,把SELINUX=enforcing改为SELINUX=dis ...