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 tries to beat his own record!
For rolling a ball one gets a score — an integer (maybe negative) number of points. Score for the i-th roll is multiplied by i and scores are summed up. So, for k rolls with scores s1, s2, ..., sk, the total score is . The total score is 0 if there were no rolls.
Limak made n rolls and got score ai for the i-th of them. He wants to maximize his total score and he came up with an interesting idea. He can say that some first rolls were only a warm-up, and that he wasn't focused during the last rolls. More formally, he can cancel any prefix and any suffix of the sequence a1, a2, ..., an. It is allowed to cancel all rolls, or to cancel none of them.
The total score is calculated as if there were only non-canceled rolls. So, the first non-canceled roll has score multiplied by 1, the second one has score multiplied by 2, and so on, till the last non-canceled roll.
What maximum total score can Limak get?
Input
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the total number of rolls made by Limak.
The second line contains n integers a1, a2, ..., an (|ai| ≤ 107) — scores for Limak's rolls.
Output
Print the maximum possible total score after cancelling rolls.
Examples
6
5 -1000 1 -3 7 -8
16
5
1000 1000 1001 1000 1000
15003
3
-60 -70 -80
0
题意:给定一段数列,现在叫你取其中一段,第一位*1,第二位*2...求最大。
思路:设前缀和和是sum[n]=a[1]+a[2]+...a[n],原先的dp[n]=a[1]*1+a[2]*2+a[3]*3+..a[n]*n;
那么现在的max[i]=max:dp[i]-dp[j]-(sum[i]-sum[j])*j; =-sum[i]*j+j*sum[j]-dp[j]+dp[j]
显然可以斜率优化,由于K=sum[i]并不是单调递增的,所以我们维护凸包不能弹出队首,而需要二分。
和BZOJ2726一样,就补多说了。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
ll s[maxn],dp[maxn],q[maxn],top,ans;
ll Y(int i){ return s[i]*i-dp[i];}
ll g(int i,int j){ return dp[i]-dp[j]-j*(s[i]-s[j]); }
int main()
{
int N,i,j;
scanf("%d",&N);
for(i=;i<=N;i++){
scanf("%I64d",&s[i]);
dp[i]=dp[i-]+s[i]*i;
s[i]+=s[i-];
ans=max(ans,dp[i]);
}
for(i=;i<=N;i++){
int L=,R=top-,Mid,t=top;
while(L<=R){
Mid=(L+R)>>;
if(g(i,q[Mid])>=g(i,q[Mid+])) t=Mid,R=Mid-;
else L=Mid+;//或者用斜率二分
}
ans=max(ans,g(i,q[t]));
while(top&&(Y(i)-Y(q[top]))*(q[top]-q[top-])>(Y(q[top])-Y(q[top-]))*(i-q[top])) top--;
q[++top]=i;
}
printf("%I64d\n",ans);
return ;
}
CodeForces - 660F:Bear and Bowling 4(DP+斜率优化)的更多相关文章
- Codeforces 660F Bear and Bowling 4 斜率优化 (看题解)
Bear and Bowling 4 这也能斜率优化... max[ i ] = a[ i ] - a[ j ] - j * (sum[ i ] - sum[ j ])然后就能斜率优化啦, 我咋没想到 ...
- 【BZOJ-4518】征途 DP + 斜率优化
4518: [Sdoi2016]征途 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 230 Solved: 156[Submit][Status][ ...
- 【BZOJ-3437】小P的牧场 DP + 斜率优化
3437: 小P的牧场 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 705 Solved: 404[Submit][Status][Discuss ...
- 【BZOJ-1010】玩具装箱toy DP + 斜率优化
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 8432 Solved: 3338[Submit][St ...
- 【BZOJ】1096: [ZJOI2007]仓库建设(dp+斜率优化)
http://www.lydsy.com/JudgeOnline/problem.php?id=1096 首先得到dp方程(我竟然自己都每推出了QAQ)$$d[i]=min\{d[j]+cost(j+ ...
- BZOJ 1096: [ZJOI2007]仓库建设(DP+斜率优化)
[ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在 ...
- 学渣乱搞系列之dp斜率优化
学渣乱搞系列之dp斜率优化 By 狂徒归来 貌似dp的斜率优化一直很难搞啊,尤其是像我这种数学很挫的学渣,压根不懂什么凸包,什么上凸下凸的,哎...说多了都是泪,跟wdd讨论了下,得出一些结论.本文很 ...
- DP斜率优化总结
目录 DP斜率优化总结 任务安排1 任务计划2 任务安排3 百日旅行 DP斜率优化总结 任务安排1 首先引入一道题,先\(O(N^2)\)做法:分别预处理出\(T_i,C_i\)前缀和\(t[i],c ...
- HDU 3507 [Print Article]DP斜率优化
题目大意 给定一个长度为\(n(n \leqslant 500000)\)的数列,将其分割为连续的若干份,使得 $ \sum ((\sum_{i=j}^kC_i) +M) $ 最小.其中\(C_i\) ...
- dp斜率优化
算法-dp斜率优化 前置知识: 凸包 斜率优化很玄学,凭空讲怎么也讲不好,所以放例题. [APIO2014]序列分割 [APIO2014]序列分割 给你一个长度为 \(n\) 的序列 \(a_1,a_ ...
随机推荐
- SQL之相关语法及操作符
概述:UNION.SELECT INTO.INSERT INTO SELECT.SQL约束 UNION操作符 UNION操作符用于合并两个或多个SELECT语句的结果集 请注意,UNION内部的每个S ...
- php设计模式中的类型安全 指--只接受特定的对象 ---以避免发生错误
在百度百科中---类型安全代码指访问被授权可以访问的内存位置
- 资源重复 uac.res resource kept(转)
一般按照网上流传的方法制作UAC.RES放到DELPHI程序里面来就可以出现盾牌.但是某些DELPHI的项目在添加了UAC.RES后编译会报错,例如: [DCC Error] E2161 Warnin ...
- redis数据迁移操作
redis客户端连接命令,分别连接旧环境中的主从redis Src目录下./redis-cli -h IP -p PORT 使用info replication 命令找出主redis使用客户端命令连接 ...
- Linux下服务端口被占用
有一次,在启动ejabberd的时候,报错如下: 10:30:15 =CRASH REPORT==== crasher: initial call: supervisor:ejabberd_liste ...
- SecureCRT的上传和下载
securtCRT对于后台开发者并不陌生,在windows下是得力的助手.而文件从服务器上上传和下载是很基本.很日常的操作.下面就谈谈关于它的命令及操作: 借助securtCRT,使用linux命令s ...
- Android开发:LocationManager获取经纬度及定位过程(附demo)
在Android开发其中.常常须要用到定位功能,尤其是依赖于地理位置功能的应用.非常多人喜欢使用百度地图,高德地图提供的sdk.开放API,可是在只须要经纬度,或者城市,街道地址等信息.并不须要提供预 ...
- linux 配置 skywalking
linux安装elasticsearch 一.检测是否已经安装的elasticsearch ps -aux|grep elasticsearch 二.下载elasticsearch (1)下载网站为: ...
- live555二次开发经验总结:RTSPClient客户端与RTSPServer服务器
live555介绍 安防领域的流媒体开发者估计没有谁不知道live555的,可能并不是因为其架构有多牛,代码有多好看,而是因为这玩意存在的年限实在是太长了,从changelog来看,live555从2 ...
- Hibernate性能优化
1.性能是与具体的项目挂钩的,并不是对于A项目某种优化方法好就适用于B项目.性能需要不断的测试检验出来的.....(废话) 2.session.clear()方法的使用,通常session是有缓存的 ...