Codeforces 631E Product Sum 斜率优化
我们先把问题分成两部分, 一部分是把元素往前移, 另一部分是把元素往后移。对于一个 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 斜率优化的更多相关文章
- @codeforces - 631E@ Product Sum
目录 @desription@ @solution@ @accepted code@ @details@ @desription@ 给定一个序列 a,定义它的权值 \(c = \sum_{i=1}^{ ...
- Codeforces 311B Cats Transport 斜率优化dp
Cats Transport 出发时间居然能是负的,我服了... 卡了我十几次, 我一直以为斜率优化写搓了. 我们能得出dp方程式 dp[ i ][ j ] = min(dp[ k ][ j - 1 ...
- Codeforces 1179D 树形DP 斜率优化
题意:给你一颗树,你可以在树上添加一条边,问添加一条边之后的简单路径最多有多少条?简单路径是指路径中的点只没有重复. 思路:添加一条边之后,树变成了基环树.容易发现,以基环上的点为根的子树的点中的简单 ...
- 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 Round #344 (Div. 2) E. Product Sum 维护凸壳
E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...
- 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 - 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 1067D - Computer Game(矩阵快速幂+斜率优化)
Codeforces 题面传送门 & 洛谷题面传送门 好题. 首先显然我们如果在某一次游戏中升级,那么在接下来的游戏中我们一定会一直打 \(b_jp_j\) 最大的游戏 \(j\),因为这样得 ...
随机推荐
- postgresql时间处理
时间取到截取 例:select date_trunc('second', "reportTime") from travel_message limit 10; 结果: 他人博客: ...
- 拿webshell权限方法(一)
概念 Webshell就是以asp,php,jsp或cgi等网页文件形式存在的一种命令执行环境,也可以将其称为一种网页后门. Access数据库拿webshell 首先看是否能找到网站漏洞进行网站的后 ...
- android 使用get和post将数据提交到服务器
1.activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android& ...
- MySQL指令
在mysql里:文件夹就是数据库 文件就是表 创建用户: 格式:create user '用户名'@'IP地址' identified by '密码'; 说明:IP地址是用来限制用户只能在哪 ...
- python技巧 计算字符串中字母出现的次数并取出最大
有一个字符串 “aaddfdfdercfghfyttefsfsfewretr123trefg5624sdfcgvfdgte6435234532”,现在需要取出里面出现次数最多的字符 第一种方法-装饰器 ...
- Java导出List集合到txt文件中——(四)
有时候,需要将数据以一定格式导出到txt文件中.利用Java的IO可以轻松的导出数据到txt中. package Action.txt; import java.io.BufferedWriter; ...
- CSS3动画常用demo
1.border动画 2.闪动动画(一闪一闪亮晶晶,满天都是小星星) .blink { animation: mymove 0.8s infinite; -webkit-animation: mymo ...
- k-means 图像分割
经典的无监督聚类算法,不多说,上代码. import numpy as np import pandas as pd import copy import matplotlib.pyplot as p ...
- Python3学习笔记07-List
Python有6个序列的内置类型,但最常见的是列表和元 序列都可以进行的操作包括索引,切片,加,乘,检查成员. 此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法. 创建一个列表, ...
- centos重启报错Umounting file systems:umount:/opt:device is busy
系统重启报错: Umounting file systems:umount:/opt:device is busy 只能硬关机,回想一下最近刚安装了nod32 for linux x64的杀毒软件,开 ...