凸包+三分 Codeforces631E roduct Sum

传送门:这里写链接内容
题意:允许交换两个位置位置的数字,最终使得 niiA[i] 最大,求最大值
思路:我们列出式子,很明显的可以发现可以用斜率优化来做。
但是我们发现A数组并不是不递减的,所以我们不能用单调队列来维护。
对于这题,我们应该去维护下凸包,然后在凸包上二分来做。
但是在做的时候,发现有非常多的细节要处理,必须非常细心才行

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int MX = 2e5 + 5;
struct Point {
    LL x, y;
    Point() {}
    Point(LL _x, LL _y) {
        x = _x; y = _y;
    }
    Point operator-(const Point &P)const {
        return Point(x - P.x, y - P.y);
    }
    LL operator*(const Point &P)const {
        return x * P.y - y * P.x;
    }
} P[MX], W[MX];
LL A[MX];
int n, sz;
LL f1(int i, int j) {
    return -P[j].y + P[i].y + A[i] * j - i * A[i];
}
LL f2(int i, int j) {
    return P[j - 1].y - P[i].y - A[j] * (j - i - 1);
}
LL solve() {
    LL ret = 0; sz = 0;
    for(int i = n; i >= 1; i--) {
        while(sz >= 2 && (P[i] - W[sz]) * (W[sz] - W[sz - 1]) <= 0) sz--;
        W[++sz] = P[i];
        int l = 1, r = sz, m1, m2;
        while(l < r) {
            m1 = (2 * l + r) / 3;
            m2 = (l + 2 * r + 2) / 3;
            if(f1(i, W[m1].x) < f1(i, W[m2].x)) l = m1 + 1;
            else r = m2 - 1;
        }
        ret = max(ret, f1(i, W[l].x));
    }
    sz = 0;
    for(int i = 1; i <= n; i++) {
        while(sz >= 2 && (P[i - 1] - W[sz]) * (W[sz] - W[sz - 1]) >= 0) sz--;
        W[++sz] = P[i - 1];
        int l = 1, r = sz, m1, m2;
        while(l < r) {
            m1 = (2 * l + r) / 3;
            m2 = (l + 2 * r + 2) / 3;
            if(f2(W[m1].x, i) < f2(W[m2].x, i)) l = m1 + 1;
            else r = m2 - 1;
        }
        ret = max(ret, f2(W[l].x, i));
    }
    return ret;
}
int main() {
    //FIN;
    scanf("%d", &n);
    LL sum = 0, pre = 0, t;
    for(int i = 1; i <= n; i++) {
        scanf("%I64d", &A[i]);
        sum += A[i];
        P[i] = Point(i, sum);
        pre += i * A[i];
    }
    printf("%I64d\n", pre + solve());
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值