D - Subsequence

发现\(f(i,j)\)不好处理,考虑将其转换成另一个函数

考虑笛卡尔树,\(\min(a_i,a_{i+1},...,a_j)\)就是在笛卡尔树上,\(i\)和\(j\)的\(lca\)

那么就可以将问题转移到笛卡尔树上,设\(dp[x][c]\)表示以\(x\)所处的子树中,选了\(c\)个的最大价值

那么显然有:

\[dp[x][i+j]=\max(dp[x][i+j],dp[ls[x]][i]+dp[rs[x]][j]-
2\times i\times j\times a[x])\\
dp[x][i+j+1]=\max(dp[x][i+j+1],dp[ls[x]][i]+
dp[rs[x]][j]+1\times m\times a[x]-(2\times i\times j+(i+j)\times 2+1)\times a[x]);
\]

复杂度:看似\(O(NM^2)\)实则\(O(NM)\)

树卷积相关理论可证

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=4e3+5;
const ll INF=1e18;
int n,m,a[N];
int ls[N],rs[N],stk[N],top,sz[N];
ll dp[N][N];
void dfs(int x){
sz[x]=1;
if(ls[x]) dfs(ls[x]),sz[x]+=sz[ls[x]];
if(rs[x]) dfs(rs[x]),sz[x]+=sz[rs[x]];
for(int i=0;i<=min(m,sz[x]);++i) dp[x][i]=-INF;
for(int i=0;i<=min(m,sz[ls[x]]);++i)
for(int j=0;j<=sz[rs[x]]&&i+j<=m;++j){
dp[x][i+j]=max(dp[x][i+j],dp[ls[x]][i]+dp[rs[x]][j]-2ll*i*j*a[x]);
if(i+j+1<=m) dp[x][i+j+1]=max(dp[x][i+j+1],dp[ls[x]][i]+dp[rs[x]][j]+1ll*m*a[x]-(2ll*i*j+(i+j)*2ll+1ll)*a[x]);
}
}
int main(){//
scanf("%d%d",&n,&m);
for(int i=1,k;i<=n;++i){
scanf("%d",&a[i]),k=top;
while(k&&a[stk[k]]>a[i]) --k;
if(k) rs[stk[k]]=i;
if(k<top) ls[i]=stk[k+1];
stk[++k]=i,top=k;
}
dfs(stk[1]);
printf("%lld",dp[stk[1]][m]); return 0;
}

[CF1580D]Subsequence的更多相关文章

  1. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  2. [LeetCode] Is Subsequence 是子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  3. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  4. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  5. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  6. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  7. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  8. CF724D. Dense Subsequence[贪心 字典序!]

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  10. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

随机推荐

  1. RAFT光流估计

    RAFT Introduction RAFT: Recurrent All-Pairs Field Transforms for Optical Flow:观其名便知道这是一篇关于光流估计的论文. 模 ...

  2. 【Docker】安装

    Docker安装 前提说明 CentOS Docker 安装 Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版本 前提条 ...

  3. AI团队比单打独斗强!CrewAI多智能体协作系统开发踩坑全解析

    AI团队比单打独斗强!CrewAI多智能体协作系统开发踩坑全解析 阅读时间: 5分钟 | 字数: 1500+ "你是否曾为单个大模型难以解决复杂专业问题而苦恼?是否想过,如果能像组建专业团队 ...

  4. Google发布A2A开源协议:“MCP+A2A”成未来标配?

    就在刚刚Google Cloud Next 25大会上,谷歌重磅开源Agent2Agent(A2A)协议,这项被类比为"AI界的HTTP协议"的技术标准,彻底打破了智能体间的信息孤 ...

  5. Cookie,Session与Token

    参考资料 水平有限,欢迎交流!仅做学习交流使用 一定要看,非常透彻![Cookie.Session.Token究竟区别在哪?如何进行身份认证,保持用户登录状态?] 黑马jwt详解 Javaweb程序设 ...

  6. grequests,并发执行接口请求的方法(简易版)

    有时候需要处理很多请求,显然,一个一个去处理是要花费很多时间的 我们就需要用到并发的方式,python并发请求的方法很多,从简单到复杂. 本案例,介绍一个超级简单,使用grequests库,实现并发请 ...

  7. MySQL 中的数据排序是怎么实现的?

    MySQL 中的数据排序实现 在 MySQL 中,数据排序是通过 ORDER BY 子句实现的,主要涉及 文件排序 和 索引排序 两种方式. 1. 排序的基本原理 MySQL 的排序实现分为以下两种情 ...

  8. .NET 原生驾驭 AI 新基建实战系列(四):Qdrant ── 实时高效的向量搜索利器

    向量数据库作为一种专为现代AI应用设计的新型存储技术,能够高效地管理和检索高维数据,成为智能应用开发中的关键基础设施.本文将深入探讨 Qdrant 这个开源.高性能的向量数据库,重点介绍其如何与 .N ...

  9. OneNote Embedded 文件滥用检测

    本文分享自天翼云开发者社区<OneNote Embedded 文件滥用检测>,作者:Icecream 攻击技术 在这些网络钓鱼活动中被滥用的OneNote功能是在图片后面隐藏嵌入式文件,诱 ...

  10. 什么是AC自动机?如何实现?

    什么是AC自动机? 是基于 Trie树 和 KMP失配指针 的一种高效多模式匹配算法.AC自动机能够一次构建,随后在遍历文本时同时匹配多个敏感词. AC自动机算法的典型应用是敏感词匹配,在各大社交媒体 ...