E. Product Sum
time limit per test:

1 second

memory limit per test:

256 megabytes

input:standard input
output:standard output

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.

Input

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.

Output

Print a single integer — the maximum possible value of characteristic of a that can be obtained by performing no more than one move.

Examples
input
4
4 3 2 5
output
39
input
5
1 1 2 7 1
output
49
input
3
1 1 2
output
9
Note

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Codeforces 631E Product Sum 斜率优化

    我们先把问题分成两部分, 一部分是把元素往前移, 另一部分是把元素往后移.对于一个 i 后的一个位置, 我们考虑前面哪个移到这里来最优. 我们设最优值为val,   val = max(a[ j ] ...

  4. CS Academy Sliding Product Sum(组合数)

    题意 有一个长为 \(N\) 的序列 \(A = [1, 2, 3, \dots, N]\) ,求所有长度 \(\le K\) 的子串权值积的和,对于 \(M\) 取模. \(N \le 10^{18 ...

  5. @codeforces - 631E@ Product Sum

    目录 @desription@ @solution@ @accepted code@ @details@ @desription@ 给定一个序列 a,定义它的权值 \(c = \sum_{i=1}^{ ...

  6. Subarray Product Less Than K LT713

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  7. 数据库sql语句规则

    sql练习: 创建一个名称为mydb1的数据库. create database mydb1; 查看库 show databases; 创建一个使用utf-8字符集的mydb2数据库. create ...

  8. MySQL高级查询 之 与 Group By 一起使用的函数 和 关键字

    1 GROUP_CONCAT mysql> SELECT student_name, ->     GROUP_CONCAT(test_score) ->     FROM stud ...

  9. HANA SQL

    约束 注释 你可以给你的 SQL 语句添加注释来增加可读性和可维护性. SQL 语句中注释的分隔如下: l  双连字符“--”.所有在双连字符之后直到行尾的内容都被 SQL 解析器认为是注释. l  ...

随机推荐

  1. tomcat的环境搭建

    tomcat搭建过程还是比较简单的,只需要安装好jdk,然后配置好环境变量,最后把tomcat安装上开启就可以了. 首先下载jdk,然后把下载下来的jdk放到/usr/local下,然后用rpm -i ...

  2. python自动化测试应用-番外篇--接口测试1

    篇1                 book-python-auto-test-番外篇--接口测试1 --lamecho辣么丑 1.1概要 大家好! 我是lamecho(辣么丑),至今<安卓a ...

  3. Javascript中变量作用域(2)

    多层函数调用取变量时,无论在哪里调用,要到创建此函数的作用域中取值,如果找不到再往上一级,直到全局变量. 外面定义了很多的全局的变量,下面我们来一个个理一下. 定义三个变量a,b,c;将A1函数赋值给 ...

  4. 新手上路---Java学习知识梳理

    我随笔 我相信也有那么一些人跟我一样,脱下军装后对未来感到迷茫,不知所措甚至怀疑起自己的能力,自身体会的经历过才能明白其中的辛酸!归咎一切:心态,信心,自信!再穷不过乞讨,不死终将出头.好了,闲言碎语 ...

  5. 用ajax判断用户是否已存在?----2017-05-12

    首先在用ajax之前,先说一下JSON: JSON:javascript object notation   js对象标记 对于json,我们只需要知道如何定义json?如何输出?怎么遍历? 1.定义 ...

  6. hadoop进不去web界面

    查看配置文件是否正确:hdfs-site.xml文件 <property> <name>dfs.http.address</name> <value>h ...

  7. ECMAScript迭代语句

    迭代语句又叫循环语句,声明一组要反复执行的命令,直到满足某些条件为止. 循环通常用于迭代数组的值(因此而得名),或者执行重复的算术任务. do-while, while, for, for-in -- ...

  8. 转:Java compiler level does not match the version of the installed Java project facet

    a.问题描述:eclipse加载新的项目后报一个错误,具体描述如下: Description Resource PathLocation Type Java compiler level does n ...

  9. 使用configuration配置结束在quartz.net中使用硬编码Job,Trigger任务提高灵活性

    经常在项目中遇到定时任务的时候,通常第一个想到的是Timer定时器,但是这玩意功能太弱鸡,实际上通常采用的是专业化的第三方调度框架,比如说 Quartz,它具有功能强大和应用的灵活性,我想使用过的人都 ...

  10. NioSocket相关知识

    一.Nio简介 nio 是non-blocking的简称,在jdk1.4 里提供的新api .Sun 官方标榜的特性如下: 为所有的原始类型提供(Buffer)缓存支持.字符集编码解码解决方案. Ch ...