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/ ...
随机推荐
- LeetCode 953 Verifying an Alien Dictionary 解题报告
题目要求 In an alien language, surprisingly they also use english lowercase letters, but possibly in a d ...
- java之获取资源文件
背景介绍 在java程序中有时我们需要加载项目中的某些资源文件(如:config.properties之类),以便获取里面的值,这样可以避免某些需要经常修改的数据硬编码入业务程序中 实现方式 实现这种 ...
- zabbix准备:php安装
一.安装php依赖库 ftp://xmlsoft.org/libxml2/libxml2-2.9.3.tar.gz yum install python-devel -y cd /download/ ...
- [MySQL优化1]centos中MySQL列出所有表
步骤1 - 连接到MySQL数据库服务器:[root@host]# mysql -u root -pEnter password:******提示登陆成功Welcome to the MySQL mo ...
- 终极大招——Scrapy框架
Scrapy框架 Scrapy 是一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途 ...
- boost生成json中的put操作
ptree中的put操作后可以加<>,指定类型,不加<>采用默认的类型,感觉不加反而更好用.用法见下面例子. #include <iostream> #includ ...
- golang 的 buffered channel 及 unbuffered channel
The channel is divided into two categories: unbuffered and buffered. (1) Unbuffered channelFor unbuf ...
- ipfs私链服务
cd /lib/systemd/system vi ipfs.service [Unit]Description=IPFS[Service]ExecStart=/usr/bin/ipfs daemon ...
- 动态添加class的一种方法
外面可以写一层class再用:class 绑定新的clss进去 而且可以用三目运算.爽歪歪
- node加密解密 crytpo
var crypto = require('crypto'); exports.encrypt = function (str, secret) { var cipher = crypto.creat ...