CF573E Bear and Bowling
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的更多相关文章
- CF573E Bear and Bowling 贪心、分块、凸包
传送门 题解搬运工++ 先证明一个贪心做法的正确性:做以下操作若干次,每一次考虑选择没有被选到答案序列中的数加入到答案序列中对答案的贡献,设第\(i\)个位置的贡献为\(V_i\),如果最大的贡献小于 ...
- CF573E Bear and Bowling(6-1)
题意 洛谷 做法一 考虑一种贪心(先别管对不对),设当前已选择的集合为\(A\),这是考虑该集合的补集,每个元素加进来后的增量为\(V_i\),则挑选最大的那个加入该集合 结论1:遵循上述贪心,\(\ ...
- 【CF573E】Bear and Bowling
[CF573E]Bear and Bowling 题面 洛谷 题解 首先有一个贪心的结论: 我们一次加入每个数,对于\(\forall i\),位置\(i\)的贡献为\(V_i = k_i\times ...
- Codeforces 660F Bear and Bowling 4 斜率优化 (看题解)
Bear and Bowling 4 这也能斜率优化... max[ i ] = a[ i ] - a[ j ] - j * (sum[ i ] - sum[ j ])然后就能斜率优化啦, 我咋没想到 ...
- 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 ...
- 牛客 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]=\ ...
- DP的优化总结
一.预备知识 \(tD/eD\) 问题:状态 t 维,决策 e 维.时间复杂度\(O(n^{e+t})\). 四边形不等式: 称代价函数 w 满足凸四边形不等式,当:\(w(a,c)+w(b,d)\l ...
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
- BUPT2017 wintertraining(16) #9
龟速补题.目前基本弃坑.已暂时放弃 D.I 两题. 下面不再写题意了直接说解法注意事项之类,直接放contest链接. https://vjudge.net/contest/151537 A.The ...
随机推荐
- linux-文件系统-5
cat /proc/partions cat /proc/mounts mount [options] -o [option] -t 文件类型 设备 挂载目录 设备: (1)设备文件:例如/dev/s ...
- HOG行人目标检测
行人检测是自动驾驶中重要的内容,对于驾驶安全具有重要意义. HOG特征提取: (1)灰度化处理 (2)Gamma变换和梯度计算 (3)Cell划分 (4)Cell组成block,归一化处理 (5)bl ...
- python build-in function
目录(?)[-] absx alliterable anyiterable basestring binx boolx callableobject chri classmethodfunction ...
- No 'Configuration' method was found in class 'WebApp.Startup
The following errors occurred while attempting to load the app.- No 'Configuration' method was found ...
- Linux root用户密码重置,远程登陆,文件基本属性
Linux root用户密码重置,远程登陆,文件基本属性 忘记Linux系统的root密码,linux系统忘记root密码的情况该怎么办呢?重新安装系统吗?当然不用!进入单用户模式更改一下root密码 ...
- CodeChef-----February Challenge 2018---Broken Clock(极坐标+三角函数递推+矩阵快速幂)
链接: https://www.codechef.com/FEB18/problems/BROCLK Broken Clock Problem Code: BROCLK Chef has a clo ...
- 挖矿病毒DDG的分析与清除
注:以下所有操作均在CentOS 7.2 x86_64位系统下完成. 今天突然收到“阿里云”的告警短信: 尊敬的****:云盾云安全中心检测到您的服务器:*.*.*.*(app)出现了紧急安全事件:挖 ...
- Python的sys.argv用法
import sys a = sys.argv[:] print("输入的参数为:", a) def train_start(start_time, end_time, selec ...
- ubuntu用mentohust连接ruijie
32位 http://download.csdn.net/detail/yan456jie/8720395 64位 http://download.csdn.net/detail/yan456jie ...
- C# AxWindowsMediaPlayer
AxWMPLib.AxWindowsMediaPlayer winPlayer = new AxWMPLib.AxWindowsMediaPlayer(); winPlayer.Dock = Dock ...