题意:给你一个数组,你可以选择数组中的一个数,把它插入数组的其它位置,问∑ i * a[i]的最大值为多少?

思路:设dp[i]表示把第i个数向左边插入可以获得的最大增量,我们假设向左边插入,设插入的位置是j,当前位置是i,那么变化为sum[i - 1] - sum[j - 1] - (i - j) * a[i], 将式子转化,sum[j - 1] = a[i] * j - dp[i] + sum[i - 1] - i * a[i],我们要让dp[i]最大,即让-dp[i]最小,用单调队列维护下凸壳,查询的时候二分斜率即可。向右边插入同理。

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 200010;
LL q[maxn], l, r;
LL a[maxn], sum[maxn];
LL dp[maxn];
int binary_search(LL k) {
if(r == l) return q[l];
int L = l, R = r;
while(L < R) {
int mid = (L + R) >> 1;
int tmp = q[mid], tmp1 = q[mid + 1];
if(sum[tmp1 - 1] - sum[tmp - 1] <= k * (tmp1 - tmp)) L = mid + 1;
else R = mid;
}
return q[L];
}
int binary_search1(LL k) {
if(r == l) return q[l];
int L = l, R = r;
while(L < R) {
int mid = (L + R) >> 1;
int tmp = q[mid], tmp1 = q[mid + 1];
if(sum[tmp1] - sum[tmp] <= k * (tmp1 - tmp)) L = mid + 1;
else R = mid;
}
return q[L];
}
int main() {
int n;
LL res = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
sum[i] = sum[i - 1] + a[i];
res = res + a[i] * i;
}
l = 1, r = 1, q[l] = 1, dp[1] = 0;
for (LL i = 2; i <= n; i++) {
int pos = binary_search(a[i]);
dp[i] = sum[i - 1] - sum[pos - 1] - (i - pos) * a[i];
while(l < r && (sum[q[r] - 1] - sum[q[r - 1] - 1]) * (i - q[r - 1]) >= (sum[i - 1] - sum[q[r - 1] - 1]) * (q[r] - q[r - 1]))r--;
q[++r] = i;
}
LL ans = -5e18;
for (int i = 1; i <= n; i++)
ans = max(ans, res + dp[i]);
l = 1, r = 1, q[1] = n;
dp[n] = 0;
for (LL i = n - 1; i >= 1; i--) {
int pos = binary_search1(a[i]);
dp[i] = sum[i] - sum[pos] - (i - pos) * a[i];
while(l < r && (sum[q[r - 1]] - sum[i]) * (q[r] - i) <= (sum[q[r]] - sum[i]) * (q[r - 1] - i))r--;
q[++r] = i;
}
for (int i = 1; i <= n; i++)
ans = max(ans, res + dp[i]);
ans = max(ans, res);
printf("%lld\n", ans);
}

  

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

  1. Codeforces 631E Product Sum 斜率优化

    我们先把问题分成两部分, 一部分是把元素往前移, 另一部分是把元素往后移.对于一个 i 后的一个位置, 我们考虑前面哪个移到这里来最优. 我们设最优值为val,   val = max(a[ j ] ...

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

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

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

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

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

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

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

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

  6. Codeforces Round #189 (Div. 1) C - Kalila and Dimna in the Logging Industry 斜率优化dp

    C - Kalila and Dimna in the Logging Industry 很容易能得到状态转移方程 dp[ i ] = min( dp[ j ] + b[ j ] * a[ i ] ) ...

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

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

  9. CodeForces 311 B Cats Transport 斜率优化DP

    题目传送门 题意:现在有n座山峰,现在 i-1 与 i 座山峰有 di长的路,现在有m个宠物, 分别在hi座山峰,第ti秒之后可以被带走,现在有p个人,每个人会从1号山峰走到n号山峰,速度1m/s.现 ...

随机推荐

  1. 怎么更新 WIN10里的SMBv1协议

    控制面板 ---启用或关闭Windows功能---打开SMBv1服务:

  2. 四、bootstrap-Table

    一.bootstrap-Table基础表格 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  3. linux 定时任务---给心爱的小姐姐发情书

    目录 1.计划任务基本概述 什么是crond? 为什么要用crond? 2.计划任务时间管理 crontab配置文件解析 crontab的时间编写规则 crontab命令选项 3.计划任务编写实践 使 ...

  4. Flask-sqlalchemy-表关系

        表关系   表之间的关系存在三种:   一对一.一对多.多对多.   而SQLAlchemy中的ORM也可以模拟这三种关系.因为一对一其实在SQLAlchemy中底层是通过一对多的方式模拟的, ...

  5. enumerate()(Python)

    >>> E=enumerate('spam') >>> E <enumerate object at 0x1021ceca8> >>> ...

  6. SparkStreaming获取kafka数据的两种方式:Receiver与Direct

    简介: Spark-Streaming获取kafka数据的两种方式-Receiver与Direct的方式,可以简单理解成: Receiver方式是通过zookeeper来连接kafka队列, Dire ...

  7. PHP closedir() 函数

    打开一个目录,读取它的内容,然后关闭: <?php$dir = "/images/"; // Open a directory, and read its contentsi ...

  8. c#获取MAC地址和IP地址

    一获取mac地址 1.先添加system.management的dll组件2.添加引用 public string GetMACAddress(){string MoAddress = "& ...

  9. kvm无人值守安装centos 7虚拟机

    centos 7安装好KVM之后还要安装虚拟机,通过VNC连接手动安装centos 7虚拟机太麻烦了,所以无人值守安装是做好的.简单记录下. 无人值守安装centos 7前提是要安装KVM,并且能手动 ...

  10. C# 语法特性

    C# 2.0 1.泛型(Generics). 2.泛型方法.泛型委托.泛型接口. 3.泛型约束(constraints). 4.部分类(partial). 5.匿名方法. C#3.0/C#3.5 1. ...