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. 一次Java后端服务间歇性响应慢的问题排查记录

    分享一个之前在公司内其它团队找到帮忙排查的一个后端服务连接超时问题,问题的表现是服务部署到线上后出现间歇性请求响应非常慢(大于10s),但是后端业务分析业务日志时却没有发现慢请求,另外由于服务容器li ...

  2. macos设置docker可以ping容器

    macos设置docker可以ping容器 项目连接不上seata 今天在启动项目时候seata报错: io.seata.common.exception.FrameworkException: ca ...

  3. Delphi字符串加密解密函数

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  4. Oracle return exit continue

    常在循环体中看到下面3种语句: return exit continue 举例说明 啥都没有 -- none begin for i in 1 .. 10 loop if i < 5 then ...

  5. golang的条件编译

    写c/c++或者rust的开发者应该对条件编译不陌生,条件编译顾名思义就是在编译时让代码中的一部分生效或者失效,从而控制编译时的代码执行路径,进而影响编译出来的程序的行为. 这有啥用呢?通常在编写跨平 ...

  6. leetcode每日一题:数组美丽值求和

    引言 ​ 今天的每日一题原题是2278. 字母在字符串中的百分比,直接模拟,逐个匹配,统计letter在原始字符串s中出现的次数,然后再计算所占百分比即可.更换成前几天遇到的更有意思的一题来写这个每日 ...

  7. 再说【把postgreSQL的表导入SQLite 】

    为这个问题,百度了一大圈.确实答案就在手边. 这个短语认识一下:[Extract-Transfrom-Load]其意义:     ETL,是英文 Extract-Transform-Load 的缩写, ...

  8. ESP32-S3接入大模型API,对话AI

    ESP32-S3接入大模型API,对话AI 1.先使用python验证可行性 import requests url = "https://api.siliconflow.cn/v1/cha ...

  9. STM32 调试小结

    图片1相关内容 确认芯片连接 使用keil软件打开一个STM32工程文档,编译,无报错 点击魔术棒,弹出配置界面"option for target XXXXX" 点击配置界面的D ...

  10. 【FAQ】HarmonyOS SDK 闭源开放能力 —Health Service Kit

    1.问题描述: 按照官方文档调用healthStore API申请用户授权:有拉起授权弹窗,但是无回调,检查权限接口也无回调. 解决方案: 1.接口调用前,需先使用init方法进行初始化,没有回调的问 ...