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\),因为这样得 ...
随机推荐
- python 小程序,输错三次密码锁定账户
[root@sun ~]# cat 7.py #!/usr/bin/python # -*- coding=UTF-8 -*- usera_name = 'usera' usera_passwd = ...
- 绕过CDN查看网站真实IP的一些办法
验证是否存在CDN最简单的办法 通过在线的多地ping,通过每个地区ping的结果得到IP. 看这些IP是否一致,如果都是一样的,极大可能不存在cdn,但不绝对. 如果这些IP大多数都不太一样或者规律 ...
- sql server存储过程简单的使用
--创建存储过程 create proc test_proc @date datetime as select * from t_user where times between ),),),),' ...
- Nginx Server 配置
http { include mime.types; // 主模块:实现对配置文件包含的文件设定,可以减少主配置文件的复杂度: default_type application/octet-strea ...
- 第18月第21日 vue cnpm
1. 首先我们需要下载npm,因为我已经提前安装了node.js,安装包里面集成了npm,然后我们就可以利用npm命令从获取淘宝镜像的cnpm了. 1.打开命令行窗口,输入 npm install - ...
- Native、Web App、Hybrid、React Native(简称RN)、Weex 间的异同点。
App常用开发模式简介 此处App为应用application,并非我们通常讲的手机App. 常用的几种APP开发模式-脑图 Native App 传统的原生App开发模式,有iOS和aOS两大系统, ...
- 改变checkbox的默认样式
针对于CheckBox默认样式的改变,和选中状态的改变 <label class="checkBox"><input type="checkbox&qu ...
- GHOST分区丢失只剩C盘
很多人装系统时会经常会使用GHOST来恢复自己的系统,当WINDOWS系统出现意外时,再用GHOST的“From Image to 分区”来对系统进行恢复,这样就可以省去繁琐耗时的重新安装操作系统的工 ...
- eclipse里访问tomcat首页出现404错误解决之法
首先,添加Tomcat.在菜单栏找到Window—Preferences—Server—Runtime Environments—Add—Apache—选择Tomcat版本—找到Tomcat文件的路径 ...
- c++ 引用方式传递数组
值传递 (pass by value),指针传递(pass by pointer),当发生函数调用时,需要给形参分配存储单元.当传递是对象时,要调用拷贝构造函数.而且指针最后析构时,要处理内存释放问题 ...