当前位置: 首页 > news >正文

Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳

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;
}

相关文章:

  • 20145237《Java程序设计》第一周学习总结
  • ListView之SimpleAdapter
  • HashMap的工作原理及HashMap和Hashtable的区别
  • 多人聊天
  • mysql5.5.48 多实例配置及启动脚本
  • java 验证码生成
  • 五大常用算法之五:分支限界法
  • Swift 中的函数(上)
  • IOS开发中UILabel单行、多行文本计算高度、宽度的技巧
  • 盲修瞎练路漫漫,名师点化三日成[转]
  • jsp 页面和 jsp标记
  • 给包文件增加注释
  • 纯手动编译安装LAMP,   cacti , nagios , zabbix
  • Maven学习 (一) 搭建Maven环境
  • 1、绪论
  • [译]CSS 居中(Center)方法大合集
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • 【前端学习】-粗谈选择器
  • 07.Android之多媒体问题
  • es6要点
  • flutter的key在widget list的作用以及必要性
  • JDK 6和JDK 7中的substring()方法
  • Nodejs和JavaWeb协助开发
  • Quartz初级教程
  • SwizzleMethod 黑魔法
  • vagrant 添加本地 box 安装 laravel homestead
  • webpack4 一点通
  • 从伪并行的 Python 多线程说起
  • 深度解析利用ES6进行Promise封装总结
  • 实现简单的正则表达式引擎
  • 使用Gradle第一次构建Java程序
  • 物联网链路协议
  • 移动端唤起键盘时取消position:fixed定位
  • 正则与JS中的正则
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • 如何用纯 CSS 创作一个货车 loader
  • # 安徽锐锋科技IDMS系统简介
  • #WEB前端(HTML属性)
  • #数学建模# 线性规划问题的Matlab求解
  • $emit传递多个参数_PPC和MIPS指令集下二进制代码中函数参数个数的识别方法
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (c语言)strcpy函数用法
  • (pojstep1.1.2)2654(直叙式模拟)
  • (附源码)spring boot儿童教育管理系统 毕业设计 281442
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • **CI中自动类加载的用法总结
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .NET 6 在已知拓扑路径的情况下使用 Dijkstra,A*算法搜索最短路径
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .NET大文件上传知识整理
  • .NET的微型Web框架 Nancy
  • .net实现客户区延伸至至非客户区