• 【文文殿下】[APIO2010]特别行动队 题解


    基本上是一个斜率优化裸题了

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn = 1e6+10;
    
    ll s[maxn],x[maxn],n,a,b,c,f[maxn],ss[maxn];
    int q[maxn];
    double slope(int l,int r) {
        double tmp1 = f[r]-b*s[r]+a*ss[r]-f[l]+b*s[l]-a*ss[l];
        double tmp2 = s[r]-s[l];
        return tmp1/tmp2;
    }
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);cout.tie(0);
        cin>>n>>a>>b>>c;
        for(int i = 1;i<=n;++i) {
            cin>>x[i];
            s[i]=s[i-1]+x[i];
            ss[i]=s[i]*s[i];
        }
        int l = 0,r=0;
        for(int i = 1;i<=n;++i) {
            while(l<r&&slope(q[l],q[l+1])>2*a*s[i]) ++l;
            int j = q[l];
            f[i]=f[j]+a*ss[i]-2*a*s[i]*s[j]+a*ss[j]+b*s[i]-b*s[j]+c;
            while(l<r&&slope(q[r-1],q[r])<slope(q[r],i)) --r;
            q[++r]=i;
        }
        cout<<f[n]<<endl;
        return 0;
    }
    
    
  • 相关阅读:
    ios 封装sqllite3接口
    ios7与ios6UI风格区别
    C/C++面试题
    单链表反转
    字符串倒序输出
    简单选择排序
    插入排序
    冒泡
    快速排序
    C++ new delete(二)
  • 原文地址:https://www.cnblogs.com/Syameimaru/p/10284855.html
Copyright © 2020-2023  润新知