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. linux服务器上配置多个svn仓库

    linux服务器上配置多个svn仓库 1.在指定目录建立仓库保存总目录,本文示例目录设定为:/usr/local/svn/svnrepos # mkdir -p /usr/local/svn/svnr ...

  2. springcloud-3:required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found.

    在写客户端程序的时候,总是报'com.netflix.discovery.DiscoveryClient' that could not be found. 原因在于导入了错误的类:com.netfl ...

  3. ACM-ICPC 2018 焦作赛区网络预赛 I Save the Room

    Bob is a sorcerer. He lives in a cuboid room which has a length of AAA, a width of BBB and a height ...

  4. Swift 学习- 04 -- 字符串和字符

    // 字符串 和 字符 // 字符串 是有序的 Character (字符) 类型的值的集合,  通过 String 类型的集合 // swift 的 String 和 Character 类型提供了 ...

  5. 处理:“ORA-28002: the password will expire within 7 days”的问题

    一:问题描述: 二:处理步骤 [oracle@localhost 2018_07_14]$ rlwrap sqlplus / as sysdba; SQL*Plus: Release 11.2.0.3 ...

  6. SpringBoot端口和访问路径

    更改启动端口:在application.properties配置文件中修改server.port=9090 更改项目访问路径:在application.properties配置文件中修改server. ...

  7. day05流程控制while循环 流程控制for循环

    1.什么是循环:循环就是重复做某事 2.为何要有循环:为了计算机能够具备人重复做某事的能力 3,.如何用循环: 1.结束while循环的两种方法:1.修改条件:等到下一次循环开始时判断,条件为假才会结 ...

  8. LeetCode(71):简化路径

    Medium! 题目描述: 给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如,path = "/home/", => "/home&quo ...

  9. CF1065D

    如果不喜欢过长代码的看官,请移步其他题解... 这题其实思想极其简单: 棋盘问题常见的算法都比较暴力,常用的有搜索和状压dp 而这道题显然没啥能状压的,所以我们考虑搜索 但是仅仅搜索是不够的,因为有极 ...

  10. 监控CPU使用率并发送报警邮件

    #!/bin/bash DATE=$(date +%F" "%H:%M) #只支持centos6 IP=$(ifconfig eth0 | awk -F '[ :]+' '/ine ...