[codeforces631E]Product Sum
1 second
256 megabytes
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
5
1 1 2 7 1
49
3
1 1 2
9
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.
题解:
刚看这个题我十分的懵逼,这怎么做?
于是我就想了个暴力O(n2)的算法:如果没有这个"exactly once"的移动,问题的答案很容易算出来,设为ans
而由于只能移动一次,所以我可以枚举移动的方案,看每种移动对答案的贡献,设为delta,选最大贡献,最终答案为ans+delta
scanf("%d",&n);
for(LL i=;i<=n;i++)
{
cin>>a[i];
s[i]=a[i]+s[i-];
ans1+=a[i]*i;
}
for(int l=;l<=n;l++)
for(int r=l+;r<=n;r++)
{
delta=max(delta,a[l]*(r-l)-(s[r]-s[l]));
delta=max(delta,s[r-]-s[l-]-a[r]*(r-l));
}
cout<<delta+ans1;
一个简单的暴力
这样肯定会t,所以我们考虑一下刚才那个暴力的式子
"delta=max(delta,a[l]*(r-l)-(s[r]-s[l]));"
"delta=max(delta,s[r-1]-s[l-1]-a[r]*(r-l));"
把这两个式子变形,可以得到:
a[l]*(r-l)-(s[r]-s[l])=a[l]*r-a[l]*l-s[r]+s[l]=(a[l]*r-s[r])+(s[l]-a[l]*l)
s[r-1]-s[l-1]-a[r]*(r-l)=a[r]*l-a[r]*r+s[r-1]-s[l-1]=(a[r]*l-s[l-1])+(s[r-1]-a[r]*r)
对于每一个l/r,右面括号里的项都是确定的,现在需要的就是确定左面括号中的最大值
而左面括号的式子形如一个k*x+b的一次函数,所以不难想到维护一个下凸壳,对于每个a[i]在下凸壳中查找
不过,这个式子并没有决策单调性,所以我们不能用单调队列维护,而是一直保存着那个下凸壳,每次用log(n)二分查找
这样就可以解决这个问题了,最后答案依然是ans+delta
代码见下:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
const int N=;
int n;
inline LL max(LL a,LL b){return a>b?a:b;}
LL a[N],s[N],ans1,delta=;
struct node{LL a,b;};
inline LL f(LL a,node b){return b.a*a+b.b;}
node q[N];int h,t;
inline LL query(LL x)
{
int le=,ri=t;
while(ri-le>)
{
int mi=(le+ri)>>;
if(f(x,q[mi])<=f(x,q[mi+]))
le=mi;
else ri=mi;
}
return f(x,q[ri]);
}
int main()
{
scanf("%d",&n);
for(LL i=;i<=n;i++)
{
cin>>a[i];
s[i]=a[i]+s[i-];
ans1+=a[i]*i;
}
t=;
for(LL r=;r<=n;r++)
{
node tmp=(node){r-,-s[r-]};
while(t>&&(q[t].b-tmp.b)*(q[t].a-q[t-].a)<=(q[t-].b-q[t].b)*(tmp.a-q[t].a))t--;
q[++t]=tmp;
delta=max(delta,query(a[r])-a[r]*r+s[r-]);
}
t=;
for(LL l=n-;l>=;l--)
{
node tmp=(node){-(l+),-s[l+]};
while(t>&&(q[t].b-tmp.b)*(q[t].a-q[t-].a)<=(q[t-].b-q[t].b)*(tmp.a-q[t].a))t--;
q[++t]=tmp;
delta=max(delta,query(-a[l])-a[l]*l+s[l]);
}
cout<<delta+ans1;
}
codeforces631E
[codeforces631E]Product Sum的更多相关文章
- 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 #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 631E Product Sum 斜率优化
我们先把问题分成两部分, 一部分是把元素往前移, 另一部分是把元素往后移.对于一个 i 后的一个位置, 我们考虑前面哪个移到这里来最优. 我们设最优值为val, val = max(a[ j ] ...
- CS Academy Sliding Product Sum(组合数)
题意 有一个长为 \(N\) 的序列 \(A = [1, 2, 3, \dots, N]\) ,求所有长度 \(\le K\) 的子串权值积的和,对于 \(M\) 取模. \(N \le 10^{18 ...
- @codeforces - 631E@ Product Sum
目录 @desription@ @solution@ @accepted code@ @details@ @desription@ 给定一个序列 a,定义它的权值 \(c = \sum_{i=1}^{ ...
- Subarray Product Less Than K LT713
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- 数据库sql语句规则
sql练习: 创建一个名称为mydb1的数据库. create database mydb1; 查看库 show databases; 创建一个使用utf-8字符集的mydb2数据库. create ...
- MySQL高级查询 之 与 Group By 一起使用的函数 和 关键字
1 GROUP_CONCAT mysql> SELECT student_name, -> GROUP_CONCAT(test_score) -> FROM stud ...
- HANA SQL
约束 注释 你可以给你的 SQL 语句添加注释来增加可读性和可维护性. SQL 语句中注释的分隔如下: l 双连字符“--”.所有在双连字符之后直到行尾的内容都被 SQL 解析器认为是注释. l ...
随机推荐
- [angularjs] MVC + Web API + AngularJs 搭建简单的 CURD 框架
MVC + Web API + AngularJs 搭建简单的 CURD 框架 GitHub 地址:https://github.com/liqingwen2015/Wen.MvcSinglePage ...
- 安装Vmware 以及 Vmware 中安装Ubuntu 以及其中问题?
在vmware中安装ubuntu 过程中 安装窗口太小,无法继续下一步? 按住alt键,用鼠标拖动linux的安装窗体,向上移动就能看到下面的确定按钮. 如何安装Vmware-tools? 1.VM菜 ...
- poj3264线段数求最大最小值
链接:https://vjudge.net/contest/66989#problem/G 完完全全的水题,还是被坑了,一个return忘了写,de了半天bug!! #include<iostr ...
- Spring Cloud构建微服务架构(一)服务注册与发现
Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...
- hdu1541 Stars 树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目大意就是统计其左上位置的星星的个数 由于y已经按升序排列,因此只用按照x坐标生成一维树状数组 ...
- 一分钟应对勒索病毒WannaCry
一.WannaCry 勒索病毒 勒索病毒WannaCry肆虐全球,利用Windows操作系统漏洞,因链式反应迅猛自动传播,校园电脑.个人电脑.政府机关都是重灾区.中毒电脑所有文档被加密,将被勒索高达3 ...
- Jquery ajaxSubmit()的浏览器兼容问题
form.ajaxSubmit({ 2 beforeSubmit: function() { 3 if (FinanceUtil.validate(form)) { 4 FinanceUtil.loa ...
- jquery之属性操作
jQuery之属性操作 相信属性这个词对大家都不陌生.今天我就给大家简单地介绍一下JQuery一些属性的操作 属性一共分三大类 一.基本属性 1.attr 2.removeAttr 3.prop 4. ...
- Android N多窗口支持
Android N 可以同时显示多个应用窗口. 在手机上,两个应用可以在"分屏"模式中左右并排或上下并排显示.例如,用户可以 在上面窗口聊QQ,下面窗口发送短信. 如图所示,两个a ...
- 在Caffe上运行Cifar10示例
准备数据集 在终端上运行以下指令: cd caffe/data/cifar10 ./get_cifar10.sh cd caffe/examples/cifar10 ./create_cifar10. ...