E. Product Sum

题目连接:

http://www.codeforces.com/contest/631/problem/E

Description

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 values ai 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.

Sample Input

4

4 3 2 5

Sample Output

39

Hint

题意

给你n个数,每个数是a[i]

然后所有数的答案就是sigma(i*a[i])

现在你可以随便交换两个数的位置

问你最后最大的答案是多少

题解:

考虑这个数和后面的数交换,那么增加的值是a[l]*(r-l)-(a[l+1]+...+a[r])

和前面的交换的话,a[r]*(l-r)+(a[l]+...+a[r-1])

然后我们分开考虑,先考虑和前面交换的

可以化简为(a[r]*l-sum[l-1])+(sum[r-1]-a[r]*r),显然我们直接暴力枚举r,然后找到一个最大的(a[r]*l-sum[l-1])就好了

这个东西可以抽象成在平面上的很多直线,斜率为l,截距为-sum[l-1]。

每一个决策可以看成平面上的点,当然我们只会选凸包上的点咯,然后不断维护这个凸包就好了。

考虑和后面交换同理。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
int n;
long long sum[maxn];
long long ans,dans;
long long a[maxn];
struct Convex_Hull
{
int sz;
pair<long long,long long> line[maxn];
void init()
{
memset(line,0,sizeof(line));
sz=0;
}
long long get(int p,long long x)
{
return line[p].first*x+line[p].second;
}
bool is_bad(long long x,long long y,long long z)
{
long long fi = (line[x].second-line[z].second)*(line[x].first-line[y].first);
long long se = (line[y].second-line[x].second)*(line[z].first-line[x].first);
return fi<=se;
}
void add(long long x,long long y)
{
line[sz++]=make_pair(x,y);
while(sz>2&&is_bad(sz-2,sz-3,sz-1))
line[sz-2]=line[sz-1],sz--;
}
long long query(long long x)
{
int l = -1 ,r = sz-1;
while(r-l>1)
{
int mid = (l+r)/2;
if(get(mid,x)<=get(mid+1,x))l=mid;
else r=mid;
}
return get(r,x);
}
}H; int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
sum[i]=sum[i-1]+a[i];
ans=ans+a[i]*i;
}
H.init();
for(int i=2;i<=n;i++)
{
H.add(i-1,-sum[i-2]);
dans=max(dans,H.query(a[i])+sum[i-1]-a[i]*i);
}
H.init();
for(int i=n-1;i>=1;i--)
{
H.add(-(i+1),-sum[i+1]);
dans=max(dans,H.query(-a[i])+sum[i]-a[i]*i);
}
cout<<ans+dans<<endl;
}

Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳的更多相关文章

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

  2. Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

    Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...

  3. Codeforces Round #344 (Div. 2)

    水 A - Interview 注意是或不是异或 #include <bits/stdc++.h> int a[1005], b[1005]; int main() { int n; sc ...

  4. 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/ ...

  5. Codeforces Round #344 (Div. 2) A

    A. Interview time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  6. Codeforces Round #344 (Div. 2) A. Interview 水题

    A. Interview 题目连接: http://www.codeforces.com/contest/631/problem/A Description Blake is a CEO of a l ...

  7. Codeforces Round #344 (Div. 2) A. Interview

    //http://codeforces.com/contest/631/problem/Apackage codeforces344; import java.io.BufferedReader; i ...

  8. Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)

     传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...

  9. Codeforces Round #232 (Div. 2) D. On Sum of Fractions

    D. On Sum of Fractions Let's assume that v(n) is the largest prime number, that does not exceed n; u ...

随机推荐

  1. peewee外键性能问题

    # 转载自:https://www.cnblogs.com/miaojiyao/articles/5217757.html 下面讨论一下用peewee的些许提高性能的方法. 避免N+1查询 N+1查询 ...

  2. 【Python学习笔记】使用Python计算皮尔逊相关系数

    源代码不记得是哪里获取的了,侵删.此处博客仅作为自己笔记学习. def multipl(a,b): sumofab=0.0 for i in range(len(a)): temp=a[i]*b[i] ...

  3. linux中使用mysql数据库

    在安装完数据库后,如果没有设置root的mysql密码,在命令行输入mysql即可进入数据库 show databases;(有分号):查看当前存在的数据库 create database 名字:创建 ...

  4. swift 动态获取类, 获取命名空间

    在做swift开发中很多时候会动态加载控制器的类, 可以让app更加灵活显示界面信息 一般情况下都是服务器返回显示的控制器类name然后动态显示, 但是服务器返回的类name是string, 怎么转换 ...

  5. 在Dubbo中使用高效的Java序列化(Kryo和FST)

    在Dubbo中使用高效的Java序列化(Kryo和FST) 作者:沈理 文档版权:Creative Commons 3.0许可证 署名-禁止演绎 完善中…… TODO 生成可点击的目录 目录 序列化漫 ...

  6. expose a port on a living Docker container

    if you have a container that with something running on its port 8000, you can run wget http://contai ...

  7. js-callee,call,apply概念

    JS - caller,callee,call,apply 概念[转载] 在提到上述的概念之前,首先想说说javascript中函数的隐含参数:arguments Arguments : 该对象代表正 ...

  8. MYSQL表中向SET类型的字段插入值时值之间不能有空格

    MYSQL 中有一种数据类型是 SET,首先我们查看一个包含 SET 类型字段的表结构: 接下来我们向表中插入数据: 按照上面的语句插入数据发现报错了,于是去掉了插入值之间的空格,然后插入成功:

  9. BNU - 49102

    进化之地(Evoland) Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO for ...

  10. 前端读者 | 前端用户体验-UI动效设计

    本文来自互联网 @羯瑞 整理 UI动效现如今在 APP 和网页中几乎已经成为了基本的组成部分,经过仔细打磨的 UI动效对于整个界面的提升是显著的. 动效呈现出状态切换的过程,展现了元素之间的逻辑关系, ...