Codeforces 631E 斜率优化
题意:给你一个数组,你可以选择数组中的一个数,把它插入数组的其它位置,问∑ 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 斜率优化的更多相关文章
- Codeforces 631E Product Sum 斜率优化
我们先把问题分成两部分, 一部分是把元素往前移, 另一部分是把元素往后移.对于一个 i 后的一个位置, 我们考虑前面哪个移到这里来最优. 我们设最优值为val, val = max(a[ j ] ...
- Codeforces 1067D - Computer Game(矩阵快速幂+斜率优化)
Codeforces 题面传送门 & 洛谷题面传送门 好题. 首先显然我们如果在某一次游戏中升级,那么在接下来的游戏中我们一定会一直打 \(b_jp_j\) 最大的游戏 \(j\),因为这样得 ...
- Codeforces 660F Bear and Bowling 4 斜率优化 (看题解)
Bear and Bowling 4 这也能斜率优化... max[ i ] = a[ i ] - a[ j ] - j * (sum[ i ] - sum[ j ])然后就能斜率优化啦, 我咋没想到 ...
- Codeforces 643C Levels and Regions 斜率优化dp
Levels and Regions 把dp方程列出来, 把所有东西拆成前缀的形式, 就能看出可以斜率优化啦. #include<bits/stdc++.h> #define LL lon ...
- Codeforces 311B Cats Transport 斜率优化dp
Cats Transport 出发时间居然能是负的,我服了... 卡了我十几次, 我一直以为斜率优化写搓了. 我们能得出dp方程式 dp[ i ][ j ] = min(dp[ k ][ j - 1 ...
- 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 ] ) ...
- 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 ...
- 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 ...
- CodeForces 311 B Cats Transport 斜率优化DP
题目传送门 题意:现在有n座山峰,现在 i-1 与 i 座山峰有 di长的路,现在有m个宠物, 分别在hi座山峰,第ti秒之后可以被带走,现在有p个人,每个人会从1号山峰走到n号山峰,速度1m/s.现 ...
随机推荐
- LeetCode Array Easy 219. Contains Duplicate II
---恢复内容开始--- Description Given an array of integers and an integer k, find out whether there are two ...
- node单线程
const fs=require('fs'); console.time('timer'); fs.stat('./1.txt',(err,stats)=>{ //console.log(sta ...
- TextView点击后背景颜色、文字颜色改变(转)
转自:http://blog.csdn.net/u013278940/article/details/51152655 TextView本没有点击效果,故为实现点击后文字颜色改变和背景改变,需要写se ...
- c# 6.0 语法特性
namespace _6._0新特性 { using static _6._0新特性.Statics.StaticClass; class Program { static void Main(str ...
- python dir()详解
Python dir() 函数 Python 内置函数 描述 dir() 函数不带参数时,返回当前范围内的变量.方法和定义的类型列表:带参数时,返回参数的属性.方法列表.如果参数包含方法__dir__ ...
- ELK Stack 7.1.1之集群搭建
一. 环境准备:3台Linux服务器,系统为CentOS 7.5 角色划分:3台机器全部安装jdk1.8,全部安装elasticsearch (后续都简称为es集群) 主节点上需要安装kibana与l ...
- kubernetes部署metrics-server metrics-server-v0.3.4 pod报错
[root@hadoop02 ~]# kubectl logs metrics-server-v0.3.4-76db4dd54b-s4t2d -c metrics-server -n kube-sys ...
- CSS 提示工具(Tooltip)
CSS 提示工具(Tooltip) 本文为大家介绍如何使用HTML和CSS来实现提示工具, 提示工具在鼠标移动到制定元素后触发,先看下面示例: 1.基础提示工具代码如下: <!doctye ht ...
- 耗时十个月的德国APS,教会我的学习方法
考过了准备了10个月的Aps ,想送给关注我的8175个粉丝,一份礼物,感谢你们看的起我,对我的支持和关注. 这份礼物,我就大言不惭的称之为:达令的学习方法. 我的考试经历:高考两次,中戏艺考三试,导 ...
- leetcode上一些常见的链表问题
92-按规定区间反转链表 思路:可以考虑成一种把前后数字的结点断开重新组合的问题 /** * Definition for singly-linked list. * struct ListNode ...