Cod sursa(job #2524424)

Utilizator Stefan_PiscuPiscu Stefan Constantin Stefan_Piscu Data 15 ianuarie 2020 17:57:20
Problema Euro Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.85 kb
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ifstream fin("euro.in");
ofstream fout("euro.out");

class CHT{
  private:
    struct func{
        ll a, b;
        ll value(ll x){
            return a*x+b;
        }
    };

    struct node{
        func val;
        node *ls, *rs;
        node(){
            ls=NULL;
            rs=NULL;
            val={0, 0};
        }
    };

    node* root=new node;

    void addline(node *&root, func line, ll l=0, ll r=(1<<30)){
        if(root==NULL) root=new node;
        ll m=(l+r)/2;
        bool mid=line.value(m)>root->val.value(m);
        bool lef=line.value(l)>root->val.value(l);
        if(mid){
            swap(root->val, line);
        }
        if(l==r) return;
        else if(mid!=lef){
            addline(root->ls, line, l, m);
        }
        else{
            addline(root->rs, line, m+1, r);
        }
    }

    ll query(node *&root, ll x, ll l=0, ll r=(1<<30)){
        if(root==NULL) return -(1<<30);
        ll m=(l+r)/2;
        if(l==r) return root->val.value(x);
        else if(x<=m){
            return max(query(root->ls, x, l, m), root->val.value(x));
        }
        else{
            return max(query(root->rs, x, m+1, r), root->val.value(x));
        }
    }
  public:

    inline void add(ll a, ll b){
        func x={a, b};
        addline(root, x);
    }

    inline ll get(ll x){
        return query(root, x);
    }

};

CHT LiChao;

int main()
{
    int n, t;
    fin>>n>>t;
    vector<ll> v(n+1, 0);
    vector<ll> s(n+1, 0);
    for(ll i=1;i<=n;++i){
        fin>>v[i];
        s[i]=s[i-1]+v[i];
    }
    vector<ll> dp(n+1, 0);
    LiChao.add(0, 0);
    for(ll i=1;i<=n;++i){
        dp[i]=s[i]*i-t+LiChao.get(i);
        LiChao.add(-s[i], dp[i]);
    }
    fout<<dp[n];
    return 0;
}