Bear and Bowling 4

这也能斜率优化。。。

max[ i ] = a[ i ] - a[ j ] - j * (sum[ i ] - sum[ j ])然后就能斜率优化啦, 我咋没想到, 我好菜啊。

斜率优化最重要的是转换成前缀形式, 我TM又忘了。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, que[N], be = , ed = ;
LL val[N], a[N], sum[N], ans; long double calc(int k, int j) {
return ((a[j] - (long double)j * sum[j]) - (a[k] - (long double)k * sum[k])) / (j - k);
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%lld", &val[i]);
a[i] = a[i - ] + i * val[i];
sum[i] = sum[i - ] + val[i];
ans = max(ans, a[i]);
}
que[++ed] = ;
for(int i = ; i <= n; i++) {
int low = be, high = ed - , p = -;
while(low <= high) {
int mid = low + high >> ;
if(calc(que[mid], que[mid + ]) < -sum[i]) p = mid, low = mid + ;
else high = mid - ;
}
int j = (p == -) ? que[be] : que[p + ];
ans = max(ans, a[i] -a[j] - j * (sum[i] - sum[j]));
while(ed > be && calc(que[ed-], que[ed]) > calc(que[ed], i)) ed--;
que[++ed] = i;
}
printf("%lld\n", ans);
return ;
} /*
*/

Codeforces 660F Bear and Bowling 4 斜率优化 (看题解)的更多相关文章

  1. 牛客 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]=\ ...

  2. Codeforces 868F Yet Another Minimization Problem 决策单调性 (看题解)

    Yet Another Minimization Problem dp方程我们很容易能得出, f[ i ] = min(g[ j ] + w( j + 1, i )). 然后感觉就根本不能优化. 然后 ...

  3. Codeforces 258D Little Elephant and Broken Sorting (看题解) 概率dp

    Little Elephant and Broken Sorting 怎么感觉这个状态好难想到啊.. dp[ i ][ j ]表示第 i 个数字比第 j 个数字大的概率.转移好像比较显然. #incl ...

  4. Codeforces 865C Gotta Go Fast 二分 + 期望dp (看题解)

    第一次看到这种骚东西, 期望还能二分的啊??? 因为存在重置的操作, 所以我们再dp的过程中有环存在. 为了消除环的影响, 我们二分dp[ 0 ][ 0 ]的值, 与通过dp得出的dp[ 0 ][ 0 ...

  5. BZOJ4409 [Usaco2016 Feb]Circular barn 动态规划 斜率优化

    原文链接http://www.cnblogs.com/zhouzhendong/p/8724739.html 题目传送门 - BZOJ4409 题意 有一个N个点的环,相邻两个点距离是1.点顺时针标号 ...

  6. bzoj1560:[JSOI2009]火星藏宝图(斜率优化)

    题目描述 在火星游玩多日,jyy偶然地发现了一张藏宝图.根据藏宝图上说法,宝藏被埋藏在一个巨大的湖里的N个岛上(2<=N<=200,000).为了方便描述,地图把整个湖划分成M行M列(1& ...

  7. 【CF573E】Bear and Bowling

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

  8. 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 ...

  9. Codeforces 1067D - Computer Game(矩阵快速幂+斜率优化)

    Codeforces 题面传送门 & 洛谷题面传送门 好题. 首先显然我们如果在某一次游戏中升级,那么在接下来的游戏中我们一定会一直打 \(b_jp_j\) 最大的游戏 \(j\),因为这样得 ...

随机推荐

  1. simulate events

    windows system maintains a msg queue, and any process that supports msg will create an thread that h ...

  2. JS访问SpringMVC后台

    var actions=ctx.path + "/api/stat/exportScheStatInfo.json"; var form = $("<form> ...

  3. python元组,集合类型,及字典补充

    一.元组 元组与列表基本相同,不同之处在于元组只能存不能取,当多个值没有改的需求时,用元组更合适 元组的基本操作 1.创建元组: t = (1, 2, 3, 4, 2,4,) t = (1,) #单个 ...

  4. hdu2602 Bone Collector 01背包

    Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like ...

  5. liunx 安装redis 4.0

    liunx 上安装redis 4.0.1 第一步:将 redis-4.0.1.tar.gz 压缩问上传至/home目录下 第二步: 解压文件  tar -zxvf  redis-4.0.1.tar.g ...

  6. sleep()和wait()的区别及wait方法的一点注意事项

    一.查看API sleep是Thread类的方法,导致此线程暂停执行指定时间,给其他线程执行机会,但是依然保持着监控状态,过了指定时间会自动恢复,调用sleep方法不会释放锁对象. 当调用sleep方 ...

  7. Windows 批处理大全(附各种实例)

    Windows 批处理大全(附各种实例) 2009年07月19日 21:31:00 阅读数:2552 批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命 ...

  8. Confluence 6 为登录失败编辑,禁用和配置验证码

    在默认的情况下,验证码将会在失败登录次数达到的时候显示. 如果为登录失败编辑,禁用和配置验证码: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链接. 在左侧 ...

  9. 【JDK】JDK模块化(1)-为什么要模块化

    Java9发布已经有一年了,跟Java8相比,从目录对比就看得出来差别相当大. 实际上Java9最大的变化就是JDK模块化(Modular). 那么,模块化的目的是什么呢? 官方的说法是: 之前版本的 ...

  10. angular基础巩固

    angular中的模块化 //定义模块 []为依赖的模块 moduleName可以使用[]模块中定义的controller filter .. var app=angular.module('modu ...