Codeforces Round #344 (Div. 2) E. Product Sum 二分斜率优化DP
Blake is the boss of Kris, however, this doesn't spoil their friendship. They often gather at the bar to talk about intriguing problems about maximising some values. This time the problem is really special.
You are given an array a of length n. The characteristic of this array is the value
— the sum of the products of the valuesai by i. One may perform the following operation exactly once: pick some element of the array and move to any position. In particular, it's allowed to move the element to the beginning or to the end of the array. Also, it's allowed to put it back to the initial position. The goal is to get the array with the maximum possible value of characteristic.

The first line of the input contains a single integer n (2 ≤ n ≤ 200 000) — the size of the array a.
The second line contains n integers ai (1 ≤ i ≤ n, |ai| ≤ 1 000 000) — the elements of the array a.
Print a single integer — the maximum possible value of characteristic of a that can be obtained by performing no more than one move.
4
4 3 2 5
39
In the first sample, one may pick the first element and place it before the third (before 5). Thus, the answer will be3·1 + 2·2 + 4·3 + 5·4 = 39.
In the second sample, one may pick the fifth element of the array and place it before the third. The answer will be1·1 + 1·2 + 1·3 + 2·4 + 7·5 = 49.
题意:
给你一个序列a,让你求∑ a[i]*i 是多少
你可以进行一次操作:将任意位置的一个数组元素拿出来再插入任意一个新的位置或者不进行此操作。
问你最大的∑ a[i]*i 是多少。
题解:
首先假设拿出元素向前面的位置插入
那么 dp[i] = max(pre[i-1]+i*a[i],pre[i-1]+sum[i-1]-sum[j-1] +j*a[i]);
pre表示前缀答案和,sum表示数组前缀和,这个转移方程是可以用斜率优化的,只不过斜率并不满足单调性质,那么我们就要手动维护一个凸包来二分找答案了。。。
拿元素向后插是一样的道理
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 1e3+, mod = 1e9+, inf = 2e9+; LL dp1[N],dp2[N],sum[N],las[N],pre[N];
LL splopex(int i,int j) {
return sum[i-] - sum[j-];
}
LL splopey(int i,int j) {
return i - j;
}
int n,a[N],q[N];
int main() {
scanf("%d",&n);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
for(int i = ; i <= n; ++i) sum[i] = sum[i-] + a[i];
int head = , tail = ;
las[] = ;
for(int i = ; i <= n; ++i) {
dp1[i] = las[i-] + 1LL * i * a[i];
las[i] = dp1[i];
if(head == tail) {q[tail++] = i;continue;}
int l = head, r = tail-,md;
while( l < r ) {
md = (l+r)>>;
if(md + < tail && splopex(q[md+],q[md]) < 1LL*splopey(q[md+],q[md])*a[i])
l = md + ;
else
r = md;
}
md = r;
dp1[i] = max(las[i-] + 1LL * sum[i-] - 1LL * sum[q[md]-] + 1LL*q[md]*(a[i]),dp1[i]);
while(head + <tail && splopey(i,q[tail-])*splopex(q[tail-],q[tail-])
>= splopey(q[tail-],q[tail-])*splopex(i,q[tail-])) tail--;
q[tail++] = i;
} pre[n+] = ;
head = , tail = ;
for(int i = n; i >= ; --i) {
dp2[i] = pre[i+] + 1LL * i * a[i];
pre[i] = dp2[i];
if(head == tail) {q[tail++] = i;continue;}
int l = head, r = tail-,md;
while( l < r ) {
md = (l+r)>>;
if(md + < tail && splopex(q[md+]+,q[md]+) < 1LL*splopey(q[md+],q[md])*a[i])
l = md + ;
else
r = md;
}
md = r;
dp2[i] = max(pre[i+] - 1LL * sum[q[md]] + 1LL * sum[i] + 1LL*q[md]*(a[i]),dp2[i]);
while(head + <tail && splopey(i,q[tail-])*splopex(q[tail-]+,q[tail-]+)
<= splopey(q[tail-],q[tail-])*splopex(i+,q[tail-]+)) tail--;
q[tail++] = i;
}
LL ans = -INF;
for(int i = ; i <= n; ++i) {
ans = max(ans, max(dp1[i]+pre[i+],dp2[i]+las[i-]));
}
cout<<ans<<endl;
return ;
}
Codeforces Round #344 (Div. 2) E. Product Sum 二分斜率优化DP的更多相关文章
- 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 Round #426 (Div. 2) D. The Bakery 线段树优化DP
D. The Bakery Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...
- Codeforces Round #587 (Div. 3) F. Wi-Fi(单调队列优化DP)
题目:https://codeforces.com/contest/1216/problem/F 题意:一排有n个位置,我要让所有点都能联网,我有两种方式联网,第一种,我直接让当前点联网,花费为i,第 ...
- Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)
Problem Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...
- Codeforces Round #344 (Div. 2)
水 A - Interview 注意是或不是异或 #include <bits/stdc++.h> int a[1005], b[1005]; int main() { int n; sc ...
- Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集
A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...
- Codeforces Round #274 (Div. 2) E. Riding in a Lift(DP)
Imagine that you are in a building that has exactly n floors. You can move between the floors in a l ...
- Codeforces Round #426 (Div. 1) B The Bakery (线段树+dp)
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp
B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...
随机推荐
- ERROR 1133 (42000): Can't find any matching row in the user table
环境:操作系统:Redhat 7.5 x86-64 数据库版本MySQL 5.7.25 现象:ERROR 1133 (42000): Can't find any matching row in th ...
- P2347 砝码称重(动态规划递推,背包,洛谷)
题目链接:P2347 砝码称重 参考题解:点击进入 纪念我第一道没理解题意的题 ''但不包括一个砝码也不用的情况'',这句话我看成了每个砝码起码放一个 然后就做不出来了 思路: 1.这题数据很小,10 ...
- MySQL与MyBatis中的查询记录
1.时间段查询 MySQL:select * from table where ctime >= CURDATE() and ctime <DATE_SUB(CURDATE(),INTER ...
- PHP:分页类(比较庞大不建议在项目中用)
文章来源:http://www.cnblogs.com/hello-tl/p/7685178.html <?php //地址 //page::$url=''; //每页的条数 默认10 //pa ...
- Python中的列表(3)
我们创建的列表元素的顺序是无法预测的,因为我们无法控制用户提供数据的顺序. 为了组织列表中的元素,所以Python帮我们提供一些方法用来排序列表中的元素. 1.方法 sort() 可以对列表永久性排序 ...
- Matlab学习笔记(四)
二.MATLAB基础知识 (六)字符串 字符串的创建和简单操作 用单引号对括起来的一系列字符的组合,每个字符是一个元素,通常通过两个字节来存储 表2-22 字符串常见操作函数(e_two_37. ...
- 洛谷P3373 线段树2(补上注释了)
毒瘤题.找了一下午+晚上的BUG,才发现原来query_tree写的是a%p; 真的是一个教训 UPD:2019.6.18 #include<iostream> #include<c ...
- CEO的作用
看到有人讨论CEO的作用. 一个观点认为CEO有三大任务: 1)为公司确定战略,并与股东沟通 2)为公司其他职位找来合适的人员 3)保证公司随时有足够的钱 他认为,可能CEO会有其他的作用,但是这三点 ...
- Thawte SSL Web Server 多域型SSL证书
Thawte SSL Web Server 多域型SSL证书,最多支持25个域名,需要验证域名所有权和申请单位信息,属于企业验证型SSL证书,提供40位/56位/128位,最高支持256位自适应加密. ...
- z_algorithm
//对于字符串a的每个后缀,匹配它与a的第一个后缀的最长公共前缀,复杂度线性void z_algorithm(char *a,int len) { z[]=len; ,j=,k;i<len;i= ...