牛客练习赛72-C brz的序列 (下凸壳,斜率优化)

题面:

思路:

我们可以推理出如下规律:

选择任意,可以使变为首项为,尾项为的等差数列。

那么本题转化为了选择若干个,作为等差数列的首尾相,使总和最小。

为了更好的解决该问题,我们把数,转为二维平面中坐标为 的点,

那么根据等差数列的性质可以得知,等差数列的公差为数列中两项对应在二维平面上两点的斜率。

通过分析可以发现,将点集维护成下凸壳(凸包的下半部分,见下图)的形式可以使答案最优。

蓝色的点是下凸壳,红色的点是应该从点集中踢出的点。

那么我们用一个栈来维护递增的斜率即可。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
ll a[maxn];
pll st[maxn];
int main()
{
#if DEBUG_Switch
    freopen("D:\\code\\input.txt", "r", stdin);
#endif
    //freopen("D:\\code\\output.txt","w",stdout);
    n = readint();
    repd(i, 1, n) {
        a[i] = readint();
    }
    if (n == 1) {
        cout << fixed << setprecision(10) << a[1] << endl;
        return 0;
    }
    int l = 0;
    int r = 0;
    repd(i, 1, n)
    {
        while (r - l > 1 && ( a[i] - st[r].first) * (i - st[r - 1].second ) < (a[i] - st[r - 1].first ) * (i - st[r].second ))
        {
            r--;
        }
        st[++r] = mp(a[i], i);
    }
    ll ans = 0ll;
    ll temp = 0ll;
    repd(i, l + 2, r)
    {
        ans += (st[i].se - st[i - 1].se + 1) * (st[i].fi + st[i - 1].fi);
        if (i >= l + 2 && i <= r - 1)
            temp += st[i].fi;
    }
    long double out = 0.5 * ans;
    out -= temp;
    cout << fixed << setprecision(10) << out << endl;
    return 0;
}