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_ ...
随机推荐
- HP Vertica Analytics Platform 评測
1.vertica概念 面向数据分析的数据仓库系统解决方式 2.vertica关键特性 Ø 标准的SQL接口:能够利用已有的BI.ETL.Hadoop/MapReduce和OLTP环境 Ø 高可用 ...
- java 中的final
在编程语言中都有某种方式,告知编译器一块数据是恒定不变的.有两个需求 1. 一个永不改变的编译器常量 2. 一个在运行时被初始化的值,而这个值不会被改变 在Java中,使用final修饰变量实现这两个 ...
- jquery单选框radio绑定click事件实现方法
本文实例讲述了jquery单选框radio绑定click事件实现方法.分享给大家供大家参考. 具体实现方法如下: 复制代码代码如下: <html><head><title ...
- Spring mybatis自动扫描dao
Spring注解方式会出现找不到dao的bean的情况 [解决方案] 在mybatis配置文件中加入以下绑定dao的方式: <!-- mapper接口namepspace绑定方式 -->& ...
- hdu 5538 House Building(长春现场赛——水题)
题目链接:acm.hdu.edu.cn/showproblem.php?pid=5538 House Building Time Limit: 2000/1000 MS (Java/Others) ...
- Quart 2D 绘制图形简单总结(转)
0 CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文 1 CGContextMoveToPoint 开始画线 2 CGConte ...
- smarty静态缓存
缓存能让程序访问起来更加快速,调数据库的数量变少,不能实时的跟数据库同步, 一般缓存文件都放在smarty文件下cach文件夹中: 建立缓存的PHP和HTML文件: 先编辑PHP文件来查询显示数据库当 ...
- WebStorm 调试JavaScript
WebStorm强大的调试JavaScript功能 Vue项目调试总结-WebStorm+Chrome调试 WebStorm+Chrome插件JetBrains IDE Support进行实时调试 W ...
- EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之_关于接口调用常见的一些问题(401 Unauthorized)
在之前的博客<EasyNVR H5流媒体服务器方案架构设计之视频能力平台>中我们描述了EasyNVR的定位,作为一个能力平台来进行功能的输出: 也就是说,在通常情况下,我们将一套视频的应用 ...
- Asp.Net Mvc: 浅析TempData机制
一. Asp.Net Mvc中的TempData 在Asp.Net Mvc框架的ControllerBase中存在一个叫做TempData的Property,它的类型为TempDataDictiona ...