Submission #406282

#TimeUsernameProblemLanguageResultExecution timeMemory
406282saarang123Discharging (NOI20_discharging)C++17
100 / 100
385 ms16472 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
 
const int inf = 1e15;
const int N = 1502;
 
using ll = long long;
ll eval(array<ll, 2> a, ll x) {
    return a[0]*x + a[1];
}
struct LiChao {
    struct Node {
        Node *l, *r;
        array<ll, 2> f;
        Node() : l(0), r(0), f{0, -(1ll<<62)} {}
    };
    deque<Node> buf;
    template<class... Args>
    Node *newnode(Args... args) {
        return &buf.emplace_back(args...);
    }
    void update(Node *&v, ll l, ll r, array<ll, 2> f) {
        if(!v) {
            v = newnode(), v->f = f;
            return;
        }
        ll mid = l + (r-l)/2;
        if(eval(f, mid) < eval(v->f, mid)) swap(f, v->f);
        if(l == r) return;
        if(eval(f, l) < eval(v->f, l))
            update(v->l, l, mid, f);
        else
            update(v->r, mid+1, r, f);
    }
    ll query(Node *v, ll l, ll r, ll x) {
        if(!v) return (1ll<<62);
        ll mid = l + (r-l)/2;
        return min(eval(v->f, x),
            x <= mid ? query(v->l, l, mid, x) : query(v->r, mid+1, r, x));
    }
    void update(array<ll, 2> f) {
        update(root, -(n-1), n-1, f);
    }
    ll query(ll x) {
        return query(root, -(n-1), n-1, x);
    }
    ll n;
    Node *root;
    LiChao(ll _n) : n(_n), root(0) {}
};

 
 
signed main() {
    std::ios::sync_with_stdio(0);
    std::cout.tie(0);
    std::cin.tie(0);
    int n; cin >> n;
    vector<int> a(n + 1), dp(n + 1);
    for(int i = 1; i <= n; i++) cin >> a[i];
    LiChao cht(1e9 + 3);
    dp[1] = a[1] * n;
    cht.update({n, 0}); //querying all n at one time
    int mx = a[1];
    for(int i = 2; i <= n; i++) {
        mx = max(mx, a[i]);
        cht.update({n - i + 1, dp[i - 1]});
        dp[i] = cht.query(mx);
        //we can qry for mx cuz the x coordinate has to be atleast mx (all under same time mx)
    }
    cout << dp[n] << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...