我们先把问题分成两部分, 一部分是把元素往前移, 另一部分是把元素往后移。对于一个 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. jQuery 选择城市,显示对应的即时时区时间

    因客户需要,我们CRM系统中,jQuery 弄个时区插件 如图: HTML: <div id="cityDate"> <i class="P_arrow ...

  2. mipsel汇编指令学习

    MIPS汇编语言基础 MIPS的系统结构及设计理念比较先进,其指令系统经过通用处理器指令体系MIPS I.MIPS II.MIPS III.MIPS IV.MIPS V,以及嵌入式指令体系MIPS16 ...

  3. jdk 环境

    一.下载 jdk 下载版本为:Linux x64 最新版本 curl -L -O http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u ...

  4. Poj3696 The Lukiest Number

    传送门 Solution 懒得写啦 Code #include<iostream> #include<cstdio> #include<cmath> #define ...

  5. [POI2007]ZAP-Queries (莫比乌斯反演+整除分块)

    [POI2007]ZAP-Queries \(solution:\) 唉,数论实在有点烂了,昨天还会的,今天就不会了,周末刚证明的,今天全忘了,还不如早点写好题解. 这题首先我们可以列出来答案就是: ...

  6. 【文件】使用word的xml模板生成.doc文件

    一.编辑模板 替换地方以变量标记如“案件编号”可写成{caseNo} template.xml 二.准备数据 以HashMap封装数据,原理是替换模板中的变量 三.替换操作 选择输出位置:writeP ...

  7. Python 入门基础17 --加密、表格、xml模块

    今日内容: 1.hashlib模块:加密 2.hmac模块:加密 3.configparser模块:操作配置文件 4.subprocess模块:操作shell命令 5.xlrd模块:excel 6.x ...

  8. 异步编程之使用yield from

    异步编程之使用yield from yield from 是 Python3.3 后新加的语言结构.yield from的主要功能是打开双向通道,把最外层的调用方法与最内层的子生成器连接起来.这两者就 ...

  9. WDS 三种模式

    (1)懒人模式(Lazy mode) 此模式下不需要填写对端的BSSID,本端AP的WDS连接作为被动连接,只需要对端填写了本端AP的BSSID即可,效果和桥接模式一样. (2)桥接模式(Bridge ...

  10. Python-CSS高级 题目

    一.简答1.完整总结display三种基础显示方式的显示方式与嵌套规则 /* inline */ /*1.同行显示, 就相当于纯文本, 当一行显示不下, 如就是一个字显示不下,那么显示不下的那一个字就 ...