我们先把问题分成两部分, 一部分是把元素往前移, 另一部分是把元素往后移。对于一个 i 后的一个位置, 我们考虑前面哪个移到这里来最优。

我们设最优值为val,   val = max(a[ j ] * (i - j) - (sum[ i ] - sum[ j ]) 我们能发现这个能转换成斜率优化的形式如果 j 比 k 更优且 j > k 我们能得到,

((j * a[ j ] - sum[ j ])  - (k * a[ k ] - sum[ k ]))  < i *  (a[ j ] - a[ k ]) ,这时候我们发现(a[ j ] - a[ k ])的符号不知道, 因为 a 不是单调的。

是我们能发现有用的 a 一定是单调递增的, 我们考虑相邻的情况,如果前面的a大, 那么它和它后一个交换肯定变优。 这样就能斜率优化啦,

反过来的情况也是一样的。

#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, head = , rear, que[N];
LL a[N], sum[N], ans; inline double calc(int k, int j) {
return (((double)j * a[j] - sum[j]) - ((double)k * a[k] - sum[k])) / (a[j] - a[k]);
} int main() {
// freopen("text.in", "r", stdin);
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%lld", &a[i]);
ans += i * a[i];
sum[i] = sum[i - ] + a[i];
}
LL tmp = ans;
for(int i = ; i <= n; i++) {
while(rear - head + >= && calc(que[head], que[head + ]) <= i) head++;
if(head <= rear) {
int who = que[head];
ans = max(ans, tmp + (i - who) * a[who] - sum[i] + sum[who]);
}
if(head > rear || (head <= rear && a[i] > a[que[rear]])) {
while(rear - head + >= && calc(que[rear - ], que[rear]) > calc(que[rear], i)) rear--;
que[++rear] = i;
}
} rear = ; head = ;
reverse(a + , a + + n);
for(int i = ; i <= n; i++) sum[i] = sum[i - ] + a[i];
for(int i = ; i <= n; i++) {
while(rear - head + >= && calc(que[head], que[head + ]) <= i) head++;
if(head <= rear) {
int who = que[head];
ans = max(ans, tmp + sum[i] - sum[who] - (i - who) * a[who]);
}
if(head > rear || (head <= rear && a[i] < a[que[rear]])) {
while(rear - head + >= && calc(que[rear - ], que[rear]) > calc(que[rear], i)) rear--;
que[++rear] = i;
}
}
printf("%lld\n", ans);
return ;
} /*
*/

Codeforces 631E Product Sum 斜率优化的更多相关文章

  1. @codeforces - 631E@ Product Sum

    目录 @desription@ @solution@ @accepted code@ @details@ @desription@ 给定一个序列 a,定义它的权值 \(c = \sum_{i=1}^{ ...

  2. Codeforces 311B Cats Transport 斜率优化dp

    Cats Transport 出发时间居然能是负的,我服了... 卡了我十几次, 我一直以为斜率优化写搓了. 我们能得出dp方程式 dp[ i ][ j ] = min(dp[ k ][ j - 1 ...

  3. Codeforces 1179D 树形DP 斜率优化

    题意:给你一颗树,你可以在树上添加一条边,问添加一条边之后的简单路径最多有多少条?简单路径是指路径中的点只没有重复. 思路:添加一条边之后,树变成了基环树.容易发现,以基环上的点为根的子树的点中的简单 ...

  4. Codeforces Round #344 (Div. 2) E. Product Sum 二分斜率优化DP

    E. Product Sum   Blake is the boss of Kris, however, this doesn't spoil their friendship. They often ...

  5. Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳

    E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...

  6. Codeforces 660F Bear and Bowling 4 斜率优化 (看题解)

    Bear and Bowling 4 这也能斜率优化... max[ i ] = a[ i ] - a[ j ] - j * (sum[ i ] - sum[ j ])然后就能斜率优化啦, 我咋没想到 ...

  7. Codeforces 643C Levels and Regions 斜率优化dp

    Levels and Regions 把dp方程列出来, 把所有东西拆成前缀的形式, 就能看出可以斜率优化啦. #include<bits/stdc++.h> #define LL lon ...

  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. Python写日志

    import logging import ResultFolder logger = logging.getLogger() logger.setLevel(logging.DEBUG) def C ...

  2. ubuntu新建组合用户命令不管用

    当我们新建了组和用户的时候发现一些命令不管用了,这是什么问题呢. 一.解决方案. 通过以下命令添加组和用户,切换到新用户,发现命令不管用,只有一个$符号. groupadd 新组名 ----添加组 u ...

  3. fatal error C1083: 无法打开包括文件: “SDKDDKVer.h”: No such file or directory(转)

    fatal error C1083: 无法打开包括文件: “SDKDDKVer.h”: No such file or directory 解决办法:(Vs2013中) 项目--右键--属性--配置属 ...

  4. Java SE之正则表达式Demo

    @Test public void regex() {//匹配教务系统课程 // String content = "公共机座 (1-10)".replaceAll(" ...

  5. windws 下 sublime Text 3 ·安装的安装与激活

    下载sublime 我们可以到官网进行下载对应的版本 https://www.sublimetext.com/3 如下是官网的内容(我选择的是Windows 64 bit). Sublime Text ...

  6. iptables学习笔记_____摘自朱双印个人日志 ____http://www.zsythink.net/

    iptables为我们预先定义了四张表 raw.mangle.nat.filter filter表负责过滤:允许那些ip访问.拒绝那些ip访问.允许那些端口...是最常用的表 #查看表里面所有的规则i ...

  7. 教你如何使用android studio发布release 版本【转】

    原文链接 想必还有人对如何在Android studio (以下简称as)发布release版本的app而狂刷百度吧?都是过来人,我很理解这种心情,百度到的基本是半成品,为什么这么说呢?百度一下,你就 ...

  8. Python-HTML CSS 练习

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. webpack中require和import的区别

    commonjs同步语法 经典的commonjs同步语法如下: var a = require('./a'); a.show(); 此时webpack会将a.js打包进引用它的文件中.这是最普遍的情形 ...

  10. Flex 布局教程转载

    Flex 布局教程:语法篇 http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html Flex 布局教程:实例篇 http://www.ruan ...