题意:给你一个数组,你可以选择数组中的一个数,把它插入数组的其它位置,问∑ 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. go语言从例子开始之Example9.切片

    Slice 是 Go 中一个关键的数据类型,是一个比数组更加强大的序列接口 package main import "fmt" func main() { 不像数组,slice 的 ...

  2. Dev常用控件

    GridControl TreeView DEV GridControl小结.. https://blog.csdn.net/happy09li/article/details/7186829 Dev ...

  3. mongodb重置密码

    1.删除服务,重新创建没有验证的服务 2.关闭服务后修改conf文件auth=false或者 3.重新开启服务后然后进入mongo 查看是否存在用户 use admin db.system.users ...

  4. vue.js axios使用

    1. 自定义配置 /** * Created by superman on 17/2/16. * http配置 */ import axios from 'axios' import utils fr ...

  5. NORDIC内核ARM蓝牙芯片NRF51802/NRF51822

    Nordic  nRF51 系列的IC 和协议堆栈对内存大小.封装类型.接口.周边产品及无线连接提供更多选择. 关于 nRF51 系列 多协议 2.4GHz 射频收发器拥有高性能.超低功耗以及灵活性等 ...

  6. hdu 5885 XM Reserves (FFT建模)

    Problem Description As an eligible Ingress Resistance Agent you should know your power source, the E ...

  7. SCP-bzoj-3309

    项目编号:bzoj-3309 项目等级:Safe 项目描述: 戳这里 特殊收容措施: 以下用\((x, y)\)表示\(gcd(x, y)\). \[ ans = \sum _ {i = 1} ^ { ...

  8. <自动化测试>之<selenium API 查找元素操作底层方法>

    搜罗了一些查找元素的除标准语句外,另外的语句使用方法,摘自 开源中国 郝云鹏driver = webdriver.Chrome(); 打开测试页面 driver.get( "http://b ...

  9. JDK各个版本比较

    JDK5 自动装箱与拆箱: 枚举 静态导入,如:import staticjava.lang.System.out 可变参数(Varargs) 内省(Introspector) 主要用于操作JavaB ...

  10. leetcode上一些常见的链表问题

    92-按规定区间反转链表 思路:可以考虑成一种把前后数字的结点断开重新组合的问题 /** * Definition for singly-linked list. * struct ListNode ...