Palindrome Bo (预处理 + 区间DP)
先进行离散化,然后再预处理出所有位置的下一个元素,做好这一步对时间的优化非常重要。
剩下的就是一般的DP了。区间DP
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; const int maxn = ;
const ll mod = 1e9 + ; ll dp[maxn][maxn], f[maxn][maxn], a[maxn];
int nex[maxn][maxn], pre[maxn][maxn];
pair<ll, int>discre[maxn]; int main(){
int n;while(~scanf("%d", &n)){
for(int i = ; i <= n; i ++){
scanf("%lld", &a[i]);
discre[i].first = a[i];
discre[i].second = i;
} sort(discre + , discre + + n);
discre[].first = -;
int cnt = ;
for(int i = ; i <= n; i ++){
if(discre[i].first != discre[i - ].first) cnt ++;
a[discre[i].second] = cnt;
}
a[] = a[n + ] = cnt + ;
memset(dp, -, sizeof(dp));
memset(nex, , sizeof(nex));
memset(pre, , sizeof(pre));
for(int i = ; i <= n + ; i ++){
for(int j = i + ; j <= n + ; j ++)
if(!nex[i][a[j]]) nex[i][a[j]] = j;
for(int j = i - ; j >= ; j --)
if(!pre[i][a[j]]) pre[i][a[j]] = j;
}
for(int l = n + ; l >= ; l -- ){
ll ans1 = , ans2 = ;
for(int r = l; r <= n + ; r ++){
ll tmp1, tmp2;
if(l == r){
dp[l][r] = f[l][r] = ;
continue;
} if(a[r - ] <= a[l]){
tmp1 = dp[nex[l][a[r-]]][pre[r-][a[r-]]];
tmp2 = f[nex[l][a[r-]]][pre[r-][a[r-]]];
if(ans1 == tmp1) ans2 = (ans2 - tmp2 + mod) % mod;
if(ans1 < tmp1) ans1 = tmp1, ans2 = tmp2;
tmp1 = dp[nex[l][a[r-]]][r - ];
tmp2 = f[nex[l][a[r-]]][r - ];
if(ans1 == tmp1) ans2 = (ans2 + tmp2) % mod;
if(ans1 < tmp1) ans1 = tmp1, ans2 = tmp2;
} if(a[l] == a[r]){
dp[l][r] = ans1 + ;
f[l][r] = ans2;
}else dp[l][r] = f[l][r] = ;
}
}
ll ans1 = , ans2 = ;
for(int i = ; i <= cnt; i ++){
if(dp[nex[][i]][pre[n+][i]] > ans1){
ans1 = dp[nex[][i]][pre[n+][i]];
ans2 = f[nex[][i]][pre[n+][i]];
}else if(ans1 == dp[nex[][i]][pre[n+][i]])
ans2 = (ans2 + f[nex[][i]][pre[n+][i]]) % mod;
}
printf("%lld %lld\n",ans1, ans2);
}
return ;
}
Palindrome Bo (预处理 + 区间DP)的更多相关文章
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
- HDU Palindrome subsequence(区间DP)
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/Oth ...
- 1044 - Palindrome Partitioning(区间DP)
题目大意: 给你一个字符串,问这个字符串最少有多少个回文串. 区间DP直接搞 #include<cstdio> #include<cstring> #include&l ...
- Palindrome subsequence(区间dp+容斥)
In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting so ...
- HDU 4632 Palindrome subsequence (区间DP)
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...
- HDU 4632 Palindrome subsequence(区间dp)
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...
- Light oj 1044 - Palindrome Partitioning(区间dp)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1044 dp[i][j]表示i到j直接的最小回文区间个数,直接看代码 #include ...
- 【HDU4632 Palindrome subsequence】区间dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题意:给你一个序列,问你该序列中有多少个回文串子序列,可以不连续. 思路:dp[i][j]表示序 ...
- hdu4632 Palindrome subsequence 回文子序列个数 区间dp
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...
随机推荐
- MSSQL2008 部署及开启远程连接
最近不少用户在windows2003 server 32位操作系统上安装SQL Server2008总是失败,出现大量错误.今天经过通过我反复测试安装,找出了一个便捷的安装方法,节省大家宝贵时间,具体 ...
- u-boot 编译,启动流程分析,移植
分析u-boot-1.1.6 的启动流程 移植u-boot 2012.04版本到JZ2440开发板 源码百度云链接:https://pan.baidu.com/s/10VnxfDWBqJVGY3SCY ...
- mysql 设置 innodb_print_all_deadlocks=ON, 保存死锁日志
Introduced 5.6.2 Command-Line Format --innodb-print-all-deadlocks=# System Variable Name innodb_prin ...
- centos7.6 安装与配置 MongoDB yum方式
1 创建yum源文件,添加以下内容 vim /etc/yum.repos.d/mongodb-org-4.0.repo [mongodb-org-4.0] name=MongoDB Repositor ...
- 001-RLE算法
一.定义 RLE全称(run-length encoding),翻译为游程编码,又译行程长度编码,又称变动长度编码法(run coding),在控制论中对于二值图像而言是一种编码方法,对连续的黑.白像 ...
- 前端文档汇总(含代码规范、开发流程、知识分享,持续更新) front-end-Doc
https://juejin.im/post/5b1205b1f265da6e1a602a62 https://juejin.im/post/5b1205b1f265da6e1a602a62 http ...
- Marathon自动扩缩容(marathon-lb-autoscale)
我们在服务里面创建如下的应用(以下是创建完复制过来的json): { "id": "/nginxtest", "cmd": null, &q ...
- HTML5服务器消息推送(java版)
前端代码(html5.html): <html> <meta http-equiv="Content-Type" content="text/html; ...
- JS通过类名判断是否都必填
//判断class='required' 是否都必填 function required() { var flag = true; $(".required").each(func ...
- 从零开始一起学习SLAM | 你好,点云
本文提纲 先热热身点云是啥你知道点云优缺点吗?点云库PCL:开发者的福音PCL安装指北炒鸡简单的PCL实践留个作业再走先热热身 小白:hi,师兄,好久不见师兄:师妹好,上周单应矩阵作业做了吗?小白:嗯 ...