先进行离散化,然后再预处理出所有位置的下一个元素,做好这一步对时间的优化非常重要。

剩下的就是一般的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)的更多相关文章

  1. LightOJ1044 Palindrome Partitioning(区间DP+线性DP)

    问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...

  2. HDU Palindrome subsequence(区间DP)

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/Oth ...

  3. 1044 - Palindrome Partitioning(区间DP)

    题目大意: 给你一个字符串,问这个字符串最少有多少个回文串. 区间DP直接搞     #include<cstdio> #include<cstring> #include&l ...

  4. Palindrome subsequence(区间dp+容斥)

    In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting so ...

  5. HDU 4632 Palindrome subsequence (区间DP)

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  6. HDU 4632 Palindrome subsequence(区间dp)

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  7. Light oj 1044 - Palindrome Partitioning(区间dp)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1044 dp[i][j]表示i到j直接的最小回文区间个数,直接看代码 #include ...

  8. 【HDU4632 Palindrome subsequence】区间dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题意:给你一个序列,问你该序列中有多少个回文串子序列,可以不连续. 思路:dp[i][j]表示序 ...

  9. hdu4632 Palindrome subsequence 回文子序列个数 区间dp

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

随机推荐

  1. LeetCode 706 Design HashMap 解题报告

    题目要求 Design a HashMap without using any built-in hash table libraries. To be specific, your design s ...

  2. LeetCode 868 Binary Gap 解题报告

    题目要求 Given a positive integer N, find and return the longest distance between two consecutive 1's in ...

  3. redis安装详解

    一.redis安装步骤: 1.首先上官网下载Redis 压缩包,地址:http://redis.io/download 下载稳定版3.0.7即可.2.通过远程管理工具,将压缩包拷贝到Linux服务器中 ...

  4. 【Python爬虫】BeautifulSoup网页解析库

    BeautifulSoup 网页解析库 阅读目录 初识Beautiful Soup Beautiful Soup库的4种解析器 Beautiful Soup类的基本元素 基本使用 标签选择器 节点操作 ...

  5. 【pyqtgraph绘图】线条,填充和颜色

    解读官方API-线条,填充和颜色 参考: http://www.pyqtgraph.org/documentation/style.html 线条,填充和颜色 Qt依靠其QColor,QPen和QBr ...

  6. MySQL+InnoDB semi-consitent read原理及实现分析(转)

    add by zhj: 主要讲的是在MySQL在Repeatable Read和Read Committed级别下,加锁时的不同,在Read Committed隔离级别下,只对where 中满足条件的 ...

  7. scss是什么?在vue.cli中的安装使用步骤是?有哪几大特性?

    css的预编译: 使用步骤: 第一步:用npm下三个loader(sass-loader.css-loader.node-sass): 第二步:在build目录找到webpack.base.confi ...

  8. SQL数据库中临时表、临时变量和WITH AS关键词创建“临时表”的区别

    原文链接:https://www.cnblogs.com/zhaowei303/articles/4204805.html SQL数据库中数据处理时,有时候需要建立临时表,将查询后的结果集放到临时表中 ...

  9. The iOS Simulator deployment target is set to 6.0

    XCODE警告 Showing All Messages :-1: The iOS Simulator deployment target is set to 6.0, but the range o ...

  10. keycloak

    keycloak报错, 少了配置项   keycloak.enabled=ture 找不到 publicKey,  1   ping不通 认证中心,2 网络不好