Link

我们设\(f_{i,j}\)表示前\(i\)个数中选\(j\)个的最大值。

那么显然有\(f_{i,j}=max(f_{i-1,j},f_{i-1,j-1}+j*a_i)\)。

这个东西我们首先可以把它的第一维给滚掉。

然后我们知道这是个\(O(n^2)\)的东西,所以要考虑优化。

有一个结论是\(\forall i\in[1,n],\exist k\in[1,i],s.t.\forall j\in[0,k),f_{i,j}=f_{i-1,j},\forall j\in[k,i],f_{i,j}=f_{i-1,j-1}+j*a_i\)

这个东西感性理解一下吧,就是你前面选的越多,选\(a_i\)时可能产生的贡献就越大。

具体证明上洛谷题解里面找吧。

那么我们每次可以把\(k\)二分出来,然后就相当于在原序列的\(f_{k-1},f_k\)之间再插一个\(f_k\)进去,后面的\(f_j\)加上一个等差数列\(a_i*j\)。

这个东西可以用平衡树来做。

#include<bits/stdc++.h>
#define lc ch[p][0]
#define rc ch[p][1]
#define ll long long
using namespace std;
int read(){int x;scanf("%d",&x);return x;}
ll max(ll a,ll b){return a>b? a:b;}
const int N=100007;
int fa[N],ch[N][2],s[N],n,root,cnt;ll A[N],B[N],val[N];
int isr(int x){return ch[fa[x]][1]==x;}
void pushup(int p){s[p]=s[lc]+s[rc]+1;}
void modify(int p,ll a,ll b){val[p]+=a*(s[lc]+1)+b,A[p]+=a,B[p]+=b;}
void pushdown(int p){ if(A[p]||B[p]) { if(lc) modify(lc,A[p],B[p]); if(rc) modify(rc,A[p],B[p]+A[p]*(s[lc]+1)); A[p]=B[p]=0; } }
void pushall(int x){if(fa[x])pushall(fa[x]);pushdown(x);}
void rotate(int x)
{
int y=fa[x],z=fa[y],k=isr(x);if(z)ch[z][isr(y)]=x;
fa[x]=z,fa[y]=x,fa[ch[x][!k]]=y,ch[y][k]=ch[x][!k],ch[x][!k]=y,pushup(y);
}
void splay(int x)
{
pushall(x);
for(;fa[x];rotate(x)) if(fa[fa[x]]) rotate((isr(x)^isr(fa[x]))? x:fa[x]);
pushup(root=x);
}
ll Kth(int k)
{
int p=root;
while(1)
if(k>s[lc]+1) k-=s[lc]+1,p=rc;
else if(s[lc]>=k) p=lc;
else return splay(p),val[p];
}
ll query(int p)
{
if(!p) return -1e18;
pushdown(p);
return max(val[p],max(query(lc),query(rc)));
}
int main()
{
n=read(),s[1]=root=cnt=1;int i,x,l,r,mid,ans;
for(i=1;i<=n;++i)
{
x=read(),l=0,r=i-2,ans=i-1;
while(l<=r){mid=l+r>>1;if(Kth(mid+1)+(mid+1ll)*x>Kth(mid+2))ans=mid,r=mid-1;else l=mid+1;}
Kth(ans+1),fa[++cnt]=root,fa[ch[root][1]]=cnt,ch[cnt][1]=ch[root][1],ch[root][1]=cnt,val[cnt]=val[root],modify(cnt,x,1ll*x*ans);
}
return !printf("%lld",query(root));
}

CF573E Bear and Bowling的更多相关文章

  1. CF573E Bear and Bowling 贪心、分块、凸包

    传送门 题解搬运工++ 先证明一个贪心做法的正确性:做以下操作若干次,每一次考虑选择没有被选到答案序列中的数加入到答案序列中对答案的贡献,设第\(i\)个位置的贡献为\(V_i\),如果最大的贡献小于 ...

  2. CF573E Bear and Bowling(6-1)

    题意 洛谷 做法一 考虑一种贪心(先别管对不对),设当前已选择的集合为\(A\),这是考虑该集合的补集,每个元素加进来后的增量为\(V_i\),则挑选最大的那个加入该集合 结论1:遵循上述贪心,\(\ ...

  3. 【CF573E】Bear and Bowling

    [CF573E]Bear and Bowling 题面 洛谷 题解 首先有一个贪心的结论: 我们一次加入每个数,对于\(\forall i\),位置\(i\)的贡献为\(V_i = k_i\times ...

  4. Codeforces 660F Bear and Bowling 4 斜率优化 (看题解)

    Bear and Bowling 4 这也能斜率优化... max[ i ] = a[ i ] - a[ j ] - j * (sum[ i ] - sum[ j ])然后就能斜率优化啦, 我咋没想到 ...

  5. CodeForces - 660F:Bear and Bowling 4(DP+斜率优化)

    Limak is an old brown bear. He often goes bowling with his friends. Today he feels really good and t ...

  6. 牛客 545A 小A与最大子段和 & CF 660F Bear and Bowling 4

    大意: 给定序列$a$, 求选择一个子区间$[l,r]$, 使得$\sum\limits_{i=l}^r(i-l+1)a_i$最大. $n\le2e5, |a_i|\le 1e7$. 记$s[i]=\ ...

  7. DP的优化总结

    一.预备知识 \(tD/eD\) 问题:状态 t 维,决策 e 维.时间复杂度\(O(n^{e+t})\). 四边形不等式: 称代价函数 w 满足凸四边形不等式,当:\(w(a,c)+w(b,d)\l ...

  8. CF数据结构练习

    1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...

  9. BUPT2017 wintertraining(16) #9

    龟速补题.目前基本弃坑.已暂时放弃 D.I 两题. 下面不再写题意了直接说解法注意事项之类,直接放contest链接. https://vjudge.net/contest/151537 A.The ...

随机推荐

  1. php大文件分片上传插件

    PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...

  2. 阿里云服务器 CentOS 7.5 64位 docker安装redis集群

    网上有很多教程可以参考,但是遇到坑了...... 最后参考这个教程成功了.https://www.cnblogs.com/hbbbs/articles/10028771.html 安装docker 参 ...

  3. 兄弟连教育分享-SQL性能优化十条经验

    1.查询的模糊匹配 尽量避免在一个复杂查询里面使用 LIKE '%parm1%'——红色标识位置的百分号会导致相关列的索引无法使用,最好不要用. 兄弟连教育分享-SQL性能优化十条经验 解决办法: 其 ...

  4. Python 爬虫十六式 - 第二式:urllib 与 urllib3

    Python请求标准库 urllib 与 urllib3 学习一时爽,一直学习一直爽!   大家好,我是 Connor,一个从无到有的技术小白.上一次我们说到了什么是HTTP协议,那么这一次我们就要动 ...

  5. SQL基础测试

    SQL 测验 结果:20/20 您的回答: 1.SQL 指的是? 您的回答:Structured Query Language 2.哪个 SQL 语句用于从数据库中提取数据? 您的回答:SELECT ...

  6. nowcoder 79F 小H和圣诞树 换根 DP + 根号分治

    设节点个数大于 $\sqrt n$ 的颜色为关键颜色,那么可以证明关键颜色最多有 $\sqrt n$ 个.对于每个关键颜色,暴力预处理出该颜色到查询中另一个颜色的距离和. 对于不是关键颜色的询问,直接 ...

  7. HDU 2795 Billboard (线段树单点更新 && 求区间最值位置)

    题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明 ...

  8. CF191C Fools and Roads - 树剖解法

    Codeforces Round #121 (Div. 1) C. Fools and Roads time limit per test :2 seconds memory limit per te ...

  9. CodeForces 1200E Compress Words

    \(C_n^m\)的typora,点了一下启用源代码模式就把我已经写好的博客弄没了,就给我留个标题,自动保存也只给我保存了个标题--\(C_n^m\),wdnmd Time limit 1000 ms ...

  10. sh_10_体验模块

    sh_10_体验模块 import sh_10_分隔线模块 sh_10_分隔线模块.print_line("-", 50) print(sh_10_分隔线模块.name)